Unerwartete Änderung der Listenwerte
Das ist meine Klasse:
class variable(object):
def __init__(self, name, name_alias, parents,values,table):
#name of the variable
self.name = ""
Das ist die Funktion mit Problemen:
f ist eine Datei .txt-Datei (geöffnet in "Hauptfunktion")
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
Meine TXT-Datei ist:
VAR
name MaryCalls
VAR
name JohnCalls
VAR
name Burglary
VAR
name Earthquake
VAR
name Alarm
Was ich in diesem Druck (und damit in der Liste) bekomme, ist:
Alarm
Alarm
Alarm
Alarm
Alarm
Aber ich wollte haben:
MaryCalls
JohnCalls
Einbruch
Erdbeben
Alarm
Was ist falsch? Warum ändern sich alle vorherigen Einträge der Liste?
Vielen Dank!