Created
August 7, 2008 09:22
-
-
Save bryanveloso/4374 to your computer and use it in GitHub Desktop.
A few examples of __dict__. Thanks Eric!
This file contains 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
# 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