Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created April 22, 2020 10:48
Show Gist options
  • Save ZhouYang1993/b021639c074a404c74e62f390d9fc58d to your computer and use it in GitHub Desktop.
Save ZhouYang1993/b021639c074a404c74e62f390d9fc58d to your computer and use it in GitHub Desktop.
Differences between Class Attribute and Instance Attribute of Python
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