Nieoczekiwana zmiana wartości listy
To moja klasa:
class variable(object):
def __init__(self, name, name_alias, parents,values,table):
#name of the variable
self.name = ""
jest to funkcja z problemami:
f jest plikiem .txt (otwieranym w „głównej funkcji”)
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
mój plik .txt to:
VAR
name MaryCalls
VAR
name JohnCalls
VAR
name Burglary
VAR
name Earthquake
VAR
name Alarm
To, co dostaję w tym druku (a więc i liście) to:
Alarm
Alarm
Alarm
Alarm
Alarm
Ale chciałem mieć:
MaryCalls
JohnCalls
Włamanie
Trzęsienie ziemi
Alarm
Co jest nie tak? Dlaczego wszystkie poprzednie wpisy na liście zmieniają się?
Dziękuję Ci!