Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Last active December 27, 2015 08:59
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 fnielsen/7300401 to your computer and use it in GitHub Desktop.
Save fnielsen/7300401 to your computer and use it in GitHub Desktop.
Instance variables vs. class (static) variables
class MyClass:
my_static_variable = "Static" # not self.
def __init__(self):
self.my_instance_variable = "Instance"
def change_static(self):
MyClass.my_static_variable = "Changed" # not self.
def change_instance(self):
self.my_instance_variable = "Also changed"
my_first_instance = MyClass()
my_second_instance = MyClass()
my_first_instance.change_static() # Will also change the second
my_first_instance.change_instance() # instance variable
my_second_instance.my_static_variable
my_second_instance.my_instance_variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment