Skip to content

Instantly share code, notes, and snippets.

@jfindlay
Created April 23, 2016 09:40
Show Gist options
  • Save jfindlay/17c83666858e0692f3f61d421a7b6260 to your computer and use it in GitHub Desktop.
Save jfindlay/17c83666858e0692f3f61d421a7b6260 to your computer and use it in GitHub Desktop.

Why are only the properties defined in __init__ listed in the __dict__ attribute of an instance of the class?

Python 3.4.3 (default, Jan 11 2016, 09:37:52)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C(object):
...     prop_1 = 'value 1'
...     def __init__(self):
...         self.prop_2 = 'value 2'
...     def set_prop_3(self):
...         return 'value 3'
...     prop_3 = property(set_prop_3)
...
>>> C.__dict__
mappingproxy({'__doc__': None, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__dict__': <attribute '__dict__' of 'C' objects>, 'prop_3': <property object at 0x7f62a1817598>, '__init__': <function C.__init__ at 0x7f62a1813840>, 'prop_1': 'value 1', '__module__': '__main__', 'set_prop_3': <function C.set_prop_3 at 0x7f62a18138c8>})
>>> C().__dict__
{'prop_2': 'value 2'}
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment