logo

Inplace vs standarta operatori Python

Vietas operatori - 1. komplekts 2. komplekts
Parastie operatori veic vienkāršu piešķiršanas darbu. No otras puses, Inplace operatori uzvedas līdzīgi kā parastie operatori izņemot ka tie darbojas citādi mainīgu un nemaināmu mērķu gadījumā. 
 

  • The _pievienot_ metode veic vienkāršu saskaitīšanu ņem divus argumentus, atgriež summu un saglabā to citā mainīgajā, nemainot nevienu no argumentiem.
  • No otras puses _iadd_ metodei ir nepieciešami arī divi argumenti, taču tā veic 1. argumenta izmaiņas, saglabājot tajā summu. Tā kā šajā procesā ir nepieciešama objektu mutācija, nemaināmi mērķi, piemēram, skaitļu virknes un korteži nedrīkst būt _iadd_ metode .
  • Parasta operatora 'add()'metode ievieš " a+b ' un saglabā rezultātu minētajā mainīgajā.Ievietojiet operatoru "iadd()"metode ievieš " a+=b ja tas pastāv (t.i., nemainīgu mērķu gadījumā tas neeksistē) un maina nodotā ​​argumenta vērtību. Bet ja nav ieviests 'a+b' .


1. gadījums : Nemainīgi mērķi.  
Nemainīgos mērķos, piemēram, skaitļu virknes un korteži. Vietas operatori darbojas tāpat kā parastie operatori, t.i., notiek tikai piešķiršana, nodotajos argumentos netiek veiktas izmaiņas.
 

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) 

Izvade:



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. gadījums : Mainīgi mērķi  
Inplace operatoru darbība mainīgos mērķos, piemēram, sarakstos un vārdnīcās, atšķiras no parastajiem operatoriem. The tiek veikta gan atjaunināšana, gan piešķiršana mainīgu mērķu gadījumā.
 

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) 

Izvade: 
 

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]


 

Izveidojiet viktorīnu