Inplace Operators - Set 1 Set 2
Normala operatörer gör det enkla tilldelningsjobbet. Å andra sidan beter Inplace-operatörer på samma sätt som vanliga operatörer utom att de agerar på ett annat sätt vid föränderliga och oföränderliga mål.
shehzad poonawala
- De _tillägga_ metoden gör enkel addition tar två argument returnerar summan och lagrar den i en annan variabel utan att ändra något av argumenten.
- Å andra sidan _iadd_ Metoden tar också två argument men den gör en ändring på plats i det första argumentet som skickas genom att lagra summan i den. Eftersom objektmutation behövs i denna process oföränderliga mål såsom nummersträngar och tupler borde inte ha _iadd_-metoden .
Fall 1 : Oföränderliga mål.
I oföränderliga mål som nummersträngar och tupler. Inplace-operatorer beter sig på samma sätt som normala operatorer, dvs endast tilldelning sker ingen modifiering av de godkända argumenten.
# 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)
Produktion:
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 : Föränderliga mål
Beteendet hos Inplace-operatorer i föränderliga mål som listor och ordböcker skiljer sig från vanliga operatorer. De både uppdatering och uppdrag genomförs vid föränderliga mål.
# 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)
Produktion:
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]