Am 07.08.24 um 11:57 schrieb Patrick Rudin über python-de:
Mike Müller wrote:
nummern = [[5,5,5]]
nummern.append([9,9,9])
for a,b,c in nummern:
print('vorher: ', id(b))
b += c
print('nachher:', id(b))
Ausgabe:
vorher: 4375044856
nachher: 4375045016
vorher: 4375044984
nachher: 4375045272
Das hilft aber nicht weiter, weil:
zahlen = [[5,5,5]]
zahlen.append([9,9,9])
for t in zahlen:
print('vorher: ', id(t[1]))
t[1] += t[2]
print('nachher:', id(t[1]))
vorher: 11743656
nachher: 11743816
vorher: 11743784
nachher: 11744072
Kurzum: Auch da wird ein neues Objekt erzeugt.
Und das neue Objekt kommt dann mit:
t[1] += t[2]
in die Liste. Aufgelöst in zwei Zeilen ist da vielleicht verständlicher:
b += t[2]
t[1] = b
Der Name `b` ist natürlich frei wählbar und könnte zum Beispiel auch `tmp`
sein.
Also anstatt den Wert eines Objektes zu modifizieren kommt ein neues Element
an der Stelle Index `1` in die Liste.
Viele Grüße
Mike
Gruss
Patrick
_______________________________________________
python-de Mailingliste -- python-de@python.org
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an
python-de-le...@python.org
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: mmuel...@python-academy.de
_______________________________________________
python-de Mailingliste -- python-de@python.org
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an
python-de-le...@python.org
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: arch...@mail-archive.com