Skip to content

Instantly share code, notes, and snippets.

@rtomaszewski
Created August 9, 2012 20:38
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 rtomaszewski/3307854 to your computer and use it in GitHub Desktop.
Save rtomaszewski/3307854 to your computer and use it in GitHub Desktop.
Differences between class and instance variables in Python
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