Created
April 22, 2020 12:23
-
-
Save ZhouYang1993/a0b9767c4b88f80774ba4820cfa955bb to your computer and use it in GitHub Desktop.
Differences between Class Attribute and Instance Attribute of Python
This file contains hidden or 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): | |
| 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