Skip to content

Instantly share code, notes, and snippets.

@lloeki
Created October 25, 2012 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloeki/3951273 to your computer and use it in GitHub Desktop.
Save lloeki/3951273 to your computer and use it in GitHub Desktop.
Something like Ruby's modules in Python
# 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