Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Last active December 12, 2015 05:18
Show Gist options
  • Select an option

  • Save kurtbrose/4720463 to your computer and use it in GitHub Desktop.

Select an option

Save kurtbrose/4720463 to your computer and use it in GitHub Desktop.
metaclass variable resolution behavior
>>> 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
@mahmoud
Copy link
Copy Markdown

mahmoud commented Feb 6, 2013

An instant modern classic PDW, in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment