Created
November 14, 2012 01:06
-
-
Save cjerdonek/4069544 to your computer and use it in GitHub Desktop.
A simple mixin example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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