Last active
December 17, 2015 09:59
-
-
Save j2labs/5591239 to your computer and use it in GitHub Desktop.
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
>>> 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