Created
October 25, 2012 07:56
-
-
Save lloeki/3951273 to your computer and use it in GitHub Desktop.
Something like Ruby's modules in Python
This file contains 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
# dynamically injecting into the multiple inheritance chain, like ruby's modules. | |
# can we stop fighting already? | |
class Foo(object): | |
pass | |
class Bar(Foo): | |
pass | |
class Baz(object): | |
# no constructor makes it like a module | |
def baz(): | |
pass | |
b = Bar() | |
print(Bar.__bases__) | |
# => (<class '__main__.Foo'>,) | |
# concatenate in any order you wish to control resolution order | |
Bar.__bases__ = Bar.__bases__ + (Baz,) | |
print(Bar.__bases__) | |
# => (<class '__main__.Foo'>, <class '__main__.Baz'>) | |
print(b.baz) | |
# => <bound method Bar.baz of <__main__.Bar object at 0x244d350>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment