Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created April 22, 2020 12:23
Show Gist options
  • Save ZhouYang1993/a0b9767c4b88f80774ba4820cfa955bb to your computer and use it in GitHub Desktop.
Save ZhouYang1993/a0b9767c4b88f80774ba4820cfa955bb to your computer and use it in GitHub Desktop.
Differences between Class Attribute and Instance Attribute of Python
class MyClass(object):
data = 0
def __init__(self, value):
self.instance_data = value
MyClass.data
# 0
my_instance = MyClass(3)
my_instance.data
# 0
my_instance.data = 10
my_instance.data
# 10
MyClass.data
# 0
my_instance.__dict__
# {'instance_data': 3, 'data': 10}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment