Skip to content

Instantly share code, notes, and snippets.

@bryanveloso
Created August 7, 2008 09:22
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 bryanveloso/4374 to your computer and use it in GitHub Desktop.
Save bryanveloso/4374 to your computer and use it in GitHub Desktop.
A few examples of __dict__. Thanks Eric!
# Let's create a dummy class
>>> class ABC(object):
... pass
...
# Let's see if it works for the class
>>> ABC.__dict__
<dictproxy object at 0x2b83b7887750>
>>> dict(ABC.__dict__)
{'__dict__': <attribute '__dict__' of 'ABC' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'ABC' objects>, '__doc__': None}
>>> ABC.x = 1
>>> dict(ABC.__dict__)
{'__dict__': <attribute '__dict__' of 'ABC' objects>, 'x': 1, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'ABC' objects>, '__doc__': None}
# Let's see if it works for the instances
>>> inst = ABC()
>>> inst.__dict__
{}
>>> inst.y = 2
>>> inst.__dict__
{'y': 2}
# However, it doesn't exist for builtins like string
>>> s = "astring"
>>> s.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__dict__'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment