Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone please help me?>

Status
Not open for further replies.

brotherjustice

Programmer
Jun 19, 2003
5
CA
I'm trying to create mulitiple objects of the same type but I think i'm shagging up the constructor. Python is new to me but I think it has potential.
here is my code

class clicbox:


def __init__(self,value=0,color="white",superpower="none",pos=0):
self.value=value
self.color=color
self.superpower=superpower
self.pos=pos
def add(self,value,color,superpower,pos):
self.value=value
self.color=color
self.superpower=superpower
self.pos=pos


class clic:
list=[]
y=clicbox()

def __init__(self,z,a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,
d3,a4,b4,c4,d4):

self.z=z
self.a1=a1
self.b1=b1
self.c1=c1
self.d1=d1

self.a2=a2
self.b2=b2
self.c2=c2
self.d2=d2

self.a3=a3
self.b3=b3
self.c3=c3
self.d3=d3

self.a4=a4
self.b4=b4
self.c4=c4
self.d4=d4

#speed
self.y.add(value=self.a1, color=self.b1, superpower=self.c1, pos=self.d1)
self.list.append(copy.deepcopy(self.y))
#attack
self.y.add(value=self.a2, color=self.b2, superpower=self.c2,pos=self.d2)
self.list.append(copy.deepcopy(self.y))
#defense
self.y.add(value=self.a3, color=self.b3, superpower=self.c3, pos=self.d3)
self.list.append(copy.deepcopy(self.y))
#damage
self.y.add(value=self.a4, color=self.b4, superpower=self.c4,pos=self.d4)
self.list.append(copy.deepcopy(self.y))

def mycopy(self,x):
return copy.deepcopy(x)
def printclic(self,x):

print self.list[0].value
print self.list[0].color
print self.list[0].superpower
print self.list[0].pos

print self.list[1].value
print self.list[1].color
print self.list[1].superpower
print self.list[1].pos

print self.list[2].value
print self.list[2].color
print self.list[2].superpower
print self.list[2].pos

print self.list[3].value
print self.list[3].color
print self.list[3].superpower
print self.list[3].pos




pass

when i run this
crap = clic(0,6,"white","none",1,7,"white","none",1,15,"white","none",1,1,"white","none",1)
print
crap.printclic(0)
crap1=clic(1,5,"white","none",2,5,"white","none",2,13,"white","none",2,1,"white","none",2)
print
crap.printclic(1)

i get this output:


6
white
none
1
7
white
none
1
15
white
none
1
1
white
none
1

6
white
none
1
7
white
none
1
15
white
none
1
1
white
none
1

I'm sure its something stupid I'm doing or not doing.
thanks for your help
 
Ok. Got it.

When you say:
[tt]class clic:
list=[]
y=clicbox()[/tt]

You're saying: "list and y are class attributes, not objects attributes."

This means that there will be only 1 list object shared among all clic class instances.
And it's the same for y.

That's why crap.printclic(0) and crap.printclic(1) give the same results.

If you want list and y to be instance-specific, you should put them in the __init__() method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top