logo

Inplace- und Standardoperatoren in Python

Inplace-Operatoren - Satz 1 Satz 2
Normale Operatoren erledigen die einfache Zuweisungsaufgabe. Andererseits verhalten sich Inplace-Operatoren ähnlich wie normale Operatoren außer dass sie bei veränderlichen und unveränderlichen Zielen unterschiedlich agieren. 
 

  • Der _hinzufügen_ Die Methode führt eine einfache Addition durch, nimmt zwei Argumente, gibt die Summe zurück und speichert sie in einer anderen Variablen, ohne eines der Argumente zu ändern.
  • Auf der anderen Seite _iadd_ Die Methode akzeptiert ebenfalls zwei Argumente, nimmt jedoch eine direkte Änderung am ersten übergebenen Argument vor, indem sie die Summe darin speichert. Als Objektmutation werden in diesem Prozess unveränderliche Ziele wie Zahlenfolgen und Tupel benötigt sollte keine _iadd_-Methode haben .
  • „add()“ des normalen OperatorsMethode implementiert ' a+b ' und speichert das Ergebnis in der genannten Variablen.'iadd()' des Inplace-OperatorsMethode implementiert ' a+=b ' wenn es existiert (d. h. im Fall unveränderlicher Ziele existiert es nicht) und ändert den Wert des übergebenen Arguments. Aber Wenn nicht, wird 'a+b' implementiert .


Fall 1 : Unveränderliche Ziele.  
In unveränderlichen Zielen wie Zahlenzeichenfolgen und Tupeln. Inplace-Operatoren verhalten sich genauso wie normale Operatoren, d. h. es findet nur eine Zuweisung statt, es werden keine Änderungen an den übergebenen Argumenten vorgenommen.
 

Python
# Python code to demonstrate difference between  # Inplace and Normal operators in Immutable Targets # importing operator to handle operator operations import operator # Initializing values x = 5 y = 6 a = 5 b = 6 # using add() to add the arguments passed  z = operator.add(ab) # using iadd() to add the arguments passed  p = operator.iadd(xy) # printing the modified value print ('Value after adding using normal operator : 'end='') print (z) # printing the modified value print ('Value after adding using Inplace operator : 'end='') print (p) # printing value of first argument # value is unchanged print ('Value of first argument using normal operator : 'end='') print (a) # printing value of first argument # value is unchanged print ('Value of first argument using Inplace operator : 'end='') print (x) 

Ausgabe:



Value after adding using normal operator : 11 Value after adding using Inplace operator : 11 Value of first argument using normal operator : 5 Value of first argument using Inplace operator : 5


Fall 2 : Veränderliche Ziele  
Das Verhalten von Inplace-Operatoren in veränderlichen Zielen wie Listen und Wörterbüchern unterscheidet sich von normalen Operatoren. Der Sowohl die Aktualisierung als auch die Zuweisung werden durchgeführt bei veränderlichen Zielen.
 

Python
# Python code to demonstrate difference between  # Inplace and Normal operators in mutable Targets # importing operator to handle operator operations import operator # Initializing list a = [1 2 4 5] # using add() to add the arguments passed  z = operator.add(a[1 2 3]) # printing the modified value print ('Value after adding using normal operator : 'end='') print (z) # printing value of first argument # value is unchanged print ('Value of first argument using normal operator : 'end='') print (a) # using iadd() to add the arguments passed  # performs a+=[1 2 3] p = operator.iadd(a[1 2 3]) # printing the modified value print ('Value after adding using Inplace operator : 'end='') print (p) # printing value of first argument # value is changed print ('Value of first argument using Inplace operator : 'end='') print (a) 

Ausgabe: 
 

Value after adding using normal operator : [1 2 4 5 1 2 3] Value of first argument using normal operator : [1 2 4 5] Value after adding using Inplace operator : [1 2 4 5 1 2 3] Value of first argument using Inplace operator : [1 2 4 5 1 2 3]


 

Quiz erstellen