[Python-de] Re: for unpacking
[email protected] (Stefan Ram) 7 Aug 2024 10:03:41 GMT
| Newsgroups | gmane.comp.python.general.german |
|---|---|
| Organization | Stefan Ram |
| Message-ID | <[email protected]> |
[email protected] (Stefan Ram) schrieb oder zitierte: >Dieses Nichtvorkommen legt es für mich nahe, daß die Frage >der Veränderbarkeit des Ziels für die Bedeutung von "+=" >nur eine begrenzte Bedeutung hat! >|An augmented assignment evaluates the target (which, unlike >|normal assignment statements, cannot be an unpacking) and the >|expression list, performs the binary operation specific to >|the type of assignment on the two operands, and assigns the >|result to the original target. >aus Abschnitt 7.2.1 Ich hatte gestern etwas übersehen! Und zwar (in bezug auf Operatoren wie "+="): |Also, when possible, the actual operation is performed |in-place, meaning that rather than creating a new object and |assigning that to the target, the old object is modified |instead. 7.2.1 . Das heißt, daß die Bedeutung der Veränderbarkeit des Ziels für "+=" (im Gegensatz zu "=") doch größer ist, als ich noch gestern abend annahm! Um das zu zeigen, betrachte ich ein Programm in zwei Versionen, einmal mit "=" und einmal mit "+=". main.py a =[ 1, 2, 3 ] original_object = a a = a +[ 4 ] print( original_object ) Ausgabe [1, 2, 3] Oben wurde das Ergebnis der Addition an den Namen "a" gebunden. Wie man an der Ausgabe sieht, wurde das ursprüngliche Object "a" dadurch aber /nicht/ verändert. Nun ersetze ich "a = a +[ 4 ]" durch "a +=[ 4 ]": main.py a =[ 1, 2, 3 ] original_object = a a +=[ 4 ] print( original_object ) Ausgabe [1, 2, 3, 4] . Der Operator "+=" hat nun die ursprüngliche Liste verändert. (In "7.2.1" besagt "assigns the result to the original target" eigentlich, daß "+=" sich hier wie "=" verhält. Erst im nächsten Absatz wird dies mit "when possible, the actual operation is performed in-place" wieder modifiziert.) _______________________________________________ python-de Mailingliste -- [email protected] Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected] https://mail.python.org/mailman3/lists/python-de.python.org/ Mitgliedsadresse: [email protected]