logo

Inplace verzus štandardní operátori v Pythone

Miestni operátori - Set 1 Súprava 2
Normálni operátori vykonávajú jednoduchú úlohu priraďovania. Na druhej strane operátori Inplace sa správajú podobne ako normálni operátori okrem že konajú iným spôsobom v prípade premenlivých a nemenných cieľov. 
 

čo je mapa java
  • The _add_ Metóda jednoduchého sčítania vezme dva argumenty, vráti súčet a uloží ho do inej premennej bez úpravy ktoréhokoľvek z argumentov.
  • Na druhej strane _iadd_ metóda tiež berie dva argumenty, ale vykoná zmenu na mieste v prvom odovzdanom argumente uložením súčtu do neho. Keďže v tomto procese je potrebná mutácia objektu, nemenné ciele, ako sú číselné reťazce a n-tice by nemal mať metódu _iadd_ .
  • 'add()' bežného operátorametóda implementuje ' a+b ' a výsledok uloží do uvedenej premennej.Vložiť operátor 'iadd()'metóda implementuje ' a+=b ' ak existuje (t. j. v prípade nemenných cieľov neexistuje) a zmení hodnotu odovzdaného argumentu. Ale ak nie je implementované „a+b“. .


Prípad 1 : Nemenné ciele.  
V Nemenné ciele, ako sú číselné reťazce a n-tice. Operátory Inplace sa správajú rovnako ako bežné operátory, t. j. prebieha iba priraďovanie, v odovzdaných argumentoch sa neuskutočňujú žiadne úpravy.
 

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) 

výstup:



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


Prípad 2 : Meniteľné ciele  
Správanie operátorov Inplace v meniteľných cieľoch, ako sú zoznamy a slovníky, sa líši od bežných operátorov. The vykonáva sa aktualizácia aj priradenie v prípade meniteľných cieľov.
 

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) 

výstup: 
 

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]


 

obrátenie reťazca v jave
Vytvoriť kvíz