Last active
December 27, 2015 08:59
-
-
Save fnielsen/7300401 to your computer and use it in GitHub Desktop.
Instance variables vs. class (static) variables
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: | |
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