Skip to content

Instantly share code, notes, and snippets.

@j2labs
Last active December 17, 2015 09:59
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 j2labs/5591239 to your computer and use it in GitHub Desktop.
Save j2labs/5591239 to your computer and use it in GitHub Desktop.
>>> class ModelMeta(type):
... def __new__(cls, name, bases, attrs):
... for base in reversed(bases):
... print 'Base:', base
... return type.__new__(cls, name, bases, attrs)
...
>>>
>>> class A(object):
... def __init__(self):
... print 'A'
... def all_three(self):
... print 'A: 3'
... def first_two(self):
... print 'A: first 2 '
...
>>>
>>> class B(object):
... def __init__(self):
... print 'B'
... def all_three(self):
... print 'B: 3'
... def first_two(self):
... print 'B: first 2'
... def last_two(self):
... print 'B: last 2'
...
>>>
>>> class C(object):
... def __init__(self):
... print 'C'
... def all_three(self):
... print 'C: 3'
... def last_two(self):
... print 'C: last 2'
... def last_one(self):
... print 'C: last 1'
...
>>>
>>> class Foo(A, B, C):
... __metaclass__ = ModelMeta
...
Base: <class '__main__.C'>
Base: <class '__main__.B'>
Base: <class '__main__.A'>
>>>
>>> f = Foo()
A
>>> f.all_three()
A: 3
>>> f.first_two()
A: first 2
>>> f.last_two()
B: last 2
>>> f.last_one()
C: last 1
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment