Helyi operátorok - 1. készlet 2. készlet
A normál operátorok elvégzik az egyszerű hozzárendelési munkát. Másrészt az Inplace operátorok a normál operátorokhoz hasonlóan viselkednek kivéve hogy változtatható és megváltoztathatatlan célpontok esetén eltérő módon járnak el.
- A _add_ metódus nem egyszerű összeadást vesz két argumentumot visszaadja az összeget, és eltárolja egy másik változóban anélkül, hogy az argumentumokat módosítaná.
- Másrészt _iadd_ A metódus két argumentumot is használ, de helyben megváltoztatja az 1. átadott argumentumot úgy, hogy eltárolja benne az összeget. Mivel ebben a folyamatban objektummutációra van szükség megváltoztathatatlan célpontokra, például számsorokra és sorokra nem szabad _iadd_ metódussal rendelkeznie .
1. eset : Megmásíthatatlan célpontok.
A megváltoztathatatlan célpontokban, például számsorokban és sorokban. Az inplace operátorok ugyanúgy viselkednek, mint a normál operátorok, azaz csak hozzárendelés történik, az átadott argumentumokban nem történik módosítás.
# 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)
Kimenet:
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
2. eset : Változó célpontok
Az Inplace operátorok viselkedése változtatható célpontokban, például listákban és szótárakban eltér a normál operátorokétól. A a frissítés és a hozzárendelés egyaránt megtörténik változó célpontok esetén.
# 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)
Kimenet:
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]