Paikkaoperaattorit - Sarja 1 Sarja 2
Normaalit operaattorit tekevät yksinkertaisen määritystyön. Toisaalta Inplace-operaattorit käyttäytyvät samalla tavalla kuin tavalliset operaattorit paitsi että ne toimivat eri tavalla muuttuvien ja muuttumattomien kohteiden tapauksessa.
- The _lisätä_ menetelmä tekee yksinkertaisen summauksen ottaa kaksi argumenttia palauttaa summan ja tallentaa sen toiseen muuttujaan muuttamatta mitään argumenteista.
- Toisaalta _iadd_ menetelmä ottaa myös kaksi argumenttia, mutta se tekee paikallaan muutoksen ensimmäiseen välitettyyn argumenttiin tallentamalla summan siihen. Koska objektimutaatiota tarvitaan tässä prosessissa muuttumattomia kohteita, kuten numerojonoja ja monikot ei pitäisi olla _iadd_-menetelmää .
Tapaus 1 : Muuttumattomat tavoitteet.
Muuttumattomissa kohteissa, kuten numerojonoissa ja monikoissa. Paikkaoperaattorit käyttäytyvät samalla tavalla kuin normaalit operaattorit, eli vain osoitus tapahtuu, ohitetuissa argumenteissa ei tapahdu muutoksia.
# 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)
Lähtö:
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
Tapaus 2 : Muuttuvat kohteet
Inplace-operaattoreiden käyttäytyminen muuttuvissa kohteissa, kuten luetteloissa ja sanakirjoissa, eroaa tavallisista operaattoreista. The sekä päivitys että toimeksianto suoritetaan muuttuvien kohteiden tapauksessa.
# 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)
Lähtö:
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]