how to declare array of class instances

Les Schaffer godzilla at netmeg.net
Mon Oct 11 09:43:00 EDT 1999


> however, all instances of trial as arr[0-n] seem to have the same
> values of local variables in the class.  i.e arr[0].tmp==arr[n].tmp

?????

#!/usr/bin/python

class trial:
    tmp=0
    def func(self,x):
	self.tmp=x
	    

arr=[]
for i in range(0,5):
    inst=trial()   #instance of trial class
    arr.append(inst)
    arr[i].func(i)

for i in range(0,5):
    print 'arr[%d].tmp = %d'%(i,arr[i].tmp)




(gustav)~/: python arrayClass.py
arr[0].tmp = 0
arr[1].tmp = 1
arr[2].tmp = 2
arr[3].tmp = 3
arr[4].tmp = 4




More information about the Python-list mailing list