logo

Vieta prieš standartinius operatorius Python

Vietos operatoriai – 1 rinkinys 2 rinkinys
Įprasti operatoriai atlieka paprastą priskyrimo darbą. Kita vertus, Inplace operatoriai elgiasi panašiai kaip įprasti operatoriai išskyrus kad jie veikia kitaip esant kintamiems ir nekintamiems taikiniams. 
 

prime be kodo java
  • The _add_ metodas atlieka paprastą sudėjimą ima du argumentus, grąžina sumą ir išsaugo ją kitame kintamajame nekeisdamas jokių argumentų.
  • Kita vertus _iadd_ metodas taip pat naudoja du argumentus, tačiau jis vietoje pakeičia 1-ąjį argumentą, išsaugodamas jame sumą. Kadangi šiame procese reikalinga objekto mutacija, nekeičiami taikiniai, tokie kaip skaičių eilutės ir eilės neturėtų turėti _iadd_ metodo .
  • Įprastas operatoriaus „add()“metodas įgyvendina a+b “ ir išsaugo rezultatą minėtame kintamajame.Įdėkite operatorių „iadd()“metodas įgyvendina a+=b “, jei jis egzistuoja (t. y. nekintančių taikinių atveju jo nėra) ir pakeičia perduodamo argumento reikšmę. Bet jei neįdiegtas „a+b“. .


1 atvejis : Nekeičiami taikiniai.  
Nekintamuose taikiniuose, pvz., skaičių eilutėse ir eilėse. Vietos operatoriai elgiasi taip pat, kaip ir įprasti operatoriai, t. y. vyksta tik priskyrimas, perduodami argumentai nekeičiami.
 

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) 

Išvestis:



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 atvejis : Kintamieji taikiniai  
Inplace operatorių elgesys kintamuose taikiniuose, pvz., sąrašuose ir žodynuose, skiriasi nuo įprastų operatorių. The vykdomas tiek atnaujinimas, tiek priskyrimas esant kintamiems taikiniams.
 

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) 

Išvestis: 
 

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]


 

Sukurti viktoriną