Inplace operaterji - Komplet 1 Komplet 2
Običajni operaterji opravijo preprosto dodeljevanje. Po drugi strani pa se operaterji Inplace obnašajo podobno kot običajni operaterji razen da delujejo na drugačen način v primeru spremenljivih in nespremenljivih ciljev.
- The _dodaj_ metoda naredi preprosto seštevanje vzame dva argumenta vrne vsoto in jo shrani v drugo spremenljivko brez spreminjanja katerega koli od argumentov.
- Na drugi strani _iadd_ metoda prav tako sprejme dva argumenta, vendar naredi spremembo na mestu v 1. posredovanem argumentu tako, da vanj shrani vsoto. Ker je v tem procesu potrebna mutacija objekta, so nespremenljivi cilji, kot so številski nizi in tuple ne bi smel imeti metode _iadd_ .
Primer 1 : Nespremenljivi cilji.
V nespremenljivih ciljih, kot so številski nizi in tuple. Operatorji na mestu se obnašajo enako kot običajni operatorji, tj. izvede se samo dodelitev, v posredovanih argumentih pa ni sprememb.
# 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)
Izhod:
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
Primer 2 : Spremenljive tarče
Vedenje operatorjev Inplace v spremenljivih ciljih, kot so seznami in slovarji, se razlikuje od običajnih operaterjev. The posodobitev in dodelitev sta izvedeni v primeru spremenljivih ciljev.
# 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)
Izhod:
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]