Skip to content

Instantly share code, notes, and snippets.

@cjerdonek
Created November 14, 2012 01:06
Show Gist options
  • Save cjerdonek/4069544 to your computer and use it in GitHub Desktop.
Save cjerdonek/4069544 to your computer and use it in GitHub Desktop.
A simple mixin example
"""
A simple mixin example.
This script illustrates using a mixin to impart a "foo" method to other
class definitions.
"""
class FooMixin(object):
def foo(self):
return "foo: %s" % repr(self)
class MySet(set, FooMixin):
pass
class MyInt(int, FooMixin):
pass
s = MySet([1, 2])
i = MyInt(100)
# Prints: "foo: MySet([1, 2])"
print(s.foo())
# Prints: "foo: 100"
print(i.foo())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment