Mudança inesperada dos valores da lista
Esta é minha aula:
class variable(object):
def __init__(self, name, name_alias, parents,values,table):
#name of the variable
self.name = ""
esta é a função com problemas:
f é um arquivo .txt (aberto em "main function")
def read_problem(f):
list_of_variables=[]
entry=0;
for line in f:
words = line.split()
#enters only if it's not a comment
if (words[0]!='#'):
if (words[0]=="VAR"):
x=variable;
elif (words[0]=="name"):
x.name=words[1]
list_of_variables.append(x)
for i in range(len(list_of_variables)):
print(list_of_variables[i].name)
return
meu arquivo .txt é:
VAR
name MaryCalls
VAR
name JohnCalls
VAR
name Burglary
VAR
name Earthquake
VAR
name Alarm
O que obtenho nessa impressão (e, portanto, na lista) é:
Alarme
Alarme
Alarme
Alarme
Alarme
Mas eu queria ter:
MaryCalls
JohnCalls
Roubo
Tremor de terra
Alarme
O que há de errado? Por que todas as entradas anteriores da lista estão mudando?
Obrigado!