I'm making my first program in Python. I need to add objects to a list, and I create these objects inside a FOR loop. The problem is, after filling the list, if I check the value of any object, they all look like the last object added.
class clsNumeros:
numero = 4
oper = [int() for i in range(5)]
def __init__(self, op1, op2, op3, op4):
self.oper[1] = op1
self.oper[2] = op2
self.oper[3] = op3
self.oper[4] = op4
def CountOperaciones():
return 5
def GetValorOper(self, idxOper):
num = self.oper[idxOper]
if num == 1:
return num
elif num == 2:
return sqrt(num)
elif num == 3:
return Factorial(num)
elif num == 4:
return 4/10
elif num == 5:
return sqrt(0.4)
def GetExprOper(self, idxOper):
num = self.oper[idxOper]
if num == 1:
return num
elif num == 2:
return "R(" + num + ")"
elif num == 3:
return num + "!"
elif num == 4:
return "0.4"
elif num == 5:
return "R(0.4)"
def Display(self):
txt = ""
for x in range(1, 5):
txt = txt + str(self.oper[x])
txt = txt + " ___ "
print (txt)
def GeneraNumeros():
c = []
idx = [int() for i in range(5)]
i = 0
idx[1] = 1
idx[2] = 1
idx[3] = 1
idx[4] = 1
looping = 1
n = 0
while looping == 1:
num = clsNumeros(idx[1], idx[2], idx[3], idx[4])
c.append(num)
n = n + 1
idx[4] = idx[4] + 1
for i in range (4, 1, -1):
if idx[i] > clsNumeros.CountOperaciones():
idx[i] = 1
idx[i-1] = idx[i-1] + 1
if idx[1] > clsNumeros.CountOperaciones():
looping = 0
c[5].Display() # this is for displaying object number 5. All of them look like the last one
return numeros
What could be wrong? Can I create objects inside a FOR loop and expect them to be unique? This idea works fine on ASP.NET.
Thanks!