logo

Inplace vs Operatori standard în Python

Operatori inlocuiți - Setul 1 Setul 2
Operatorii normali fac sarcina de atribuire simplă. Pe de altă parte, operatorii Inplace se comportă similar cu operatorii normali cu excepţia că acţionează într-un mod diferit în cazul ţintelor mutabile şi Imuabile. 
 

îmbinări și tipuri de îmbinări
  • The _adăuga_ metoda face adunarea simplă ia două argumente returnează suma și o stochează într-o altă variabilă fără a modifica niciunul dintre argumente.
  • Pe de altă parte _iadd_ metoda ia, de asemenea, două argumente, dar efectuează o schimbare în loc în primul argument transmis prin stocarea sumei în el. Deoarece mutația obiectului este necesară în acest proces, ținte imuabile, cum ar fi șiruri de numere și tupluri nu ar trebui să aibă metoda _iadd_ .
  • Operatorul normal „add()”implementează metoda " a+b ' și stochează rezultatul în variabila menționată.„iadd()” al operatorului de înlocuireimplementează metoda " a+=b ' dacă există (adică în cazul țintelor imuabile, nu există) și schimbă valoarea argumentului transmis. Dar dacă nu este implementat „a+b”. .


Cazul 1 : Ținte imuabile.  
În ținte imuabile, cum ar fi șiruri de numere și tupluri. Operatorii inplace se comportă la fel ca operatorii normali, adică numai atribuirea are loc, nu are loc nicio modificare în argumentele transmise.
 

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) 

Ieșire:



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


Cazul 2 : Ținte mutabile  
Comportamentul operatorilor Inplace în ținte mutabile, cum ar fi liste și dicționare, este diferit de operatorii normali. The actualizarea și atribuirea ambele sunt efectuate în cazul țintelor mutabile.
 

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) 

Ieșire: 
 

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]


 

setările browserului web
Creați un test