Created
April 22, 2020 10:48
-
-
Save ZhouYang1993/b021639c074a404c74e62f390d9fc58d to your computer and use it in GitHub Desktop.
Differences between Class Attribute and Instance Attribute of Python
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
class MyClass(object): | |
class_attr = 0 | |
def __init__(self, instance_attr): | |
self.instance_attr = instance_attr | |
print(MyClass.__dict__) | |
# {'__module__': '__main__', 'class_attr': 0, '__init__': <function MyClass.__init__ at 0x7f00cb7fcea0>, '__dict__': <attribute '__dict__' of 'MyClass' objects>, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None} | |
my_instance = MyClass(1) | |
print(my_instance.__dict__) | |
# {'instance_attr': 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment