Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created February 21, 2014 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaptur/9137899 to your computer and use it in GitHub Desktop.
Save akaptur/9137899 to your computer and use it in GitHub Desktop.
Class variables vs. instance variables in python
>>> class Test(object):
... var = 7
... def __init__(self):
... self.ivar = 2
...
>>> t = Test()
>>> s = Test()
>>> t.__dict__
{'ivar': 2}
>>> t.var
7
>>> t.var = 8
>>> t.__dict__
{'var': 8, 'ivar': 2}
>>> t.var
8
>>> s.var
7
>>> Test.var = 10
>>> s.var
10
>>> x = Test()
>>> x.var
10
>>> t.var
8
>>> t.__dict__
{'var': 8, 'ivar': 2}
>>> s.__dict__
{'ivar': 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment