Last active
December 12, 2015 05:18
-
-
Save kurtbrose/4720463 to your computer and use it in GitHub Desktop.
metaclass variable resolution behavior
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 A(type): | |
| ... @property | |
| ... def a(cls): return 4 | |
| ... | |
| >>> class B1(object): | |
| ... __metaclass__ = A | |
| ... a = 3 | |
| ... | |
| >>> B1.a | |
| 4 | |
| >>> class C(type): | |
| ... a = 2 | |
| ... | |
| >>> class D(object): | |
| ... __metaclass__ = C | |
| ... a = 1 | |
| ... | |
| >>> D.a | |
| 1 | |
| >>> class E(object): | |
| ... @property | |
| ... def e(self): return 2 | |
| ... | |
| >>> E.e | |
| <property object at 0x02371570> | |
| >>> e = E() | |
| >>> e.e = 1 | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> | |
| AttributeError: can't set attribute | |
| >>> e.__dict__['e'] = 1 | |
| >>> e.e | |
| 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An instant modern classic PDW, in my opinion.