Created
August 9, 2012 20:38
-
-
Save rtomaszewski/3307854 to your computer and use it in GitHub Desktop.
Differences between class and instance variables in 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 Test1InstanceVariable: | |
mylist=[] | |
def __init__(self, number): | |
self.number= number | |
def add(self, i): | |
self.mylist.append(i) | |
def show(self): | |
print "[test %d] list=%s" % (self.number, " ".join(self.mylist)) | |
class Test2ClassVariable: | |
mylist=[] | |
def __init__(self, number): | |
self.number= number | |
self.mylist=[] | |
def add(self, i): | |
self.mylist.append(i) | |
def show(self): | |
print "[test %d] list=%s" % (self.number, " ".join(self.mylist)) | |
if __name__ == '__main__': | |
t1=Test1InstanceVariable(1) | |
t1.add('one') | |
t1.show() | |
t1=Test1InstanceVariable(2) | |
t1.add('two') | |
t1.show() | |
t1=Test2ClassVariable(3) | |
t1.add('three') | |
t1.show() | |
t1=Test2ClassVariable(4) | |
t1.add('four') | |
t1.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment