logo

Inplace a operatory standardowe w Pythonie

Operatorzy lokalni — Zestaw 1 Zestaw 2
Zwykli operatorzy wykonują proste zadanie przypisywania. Z drugiej strony operatorzy Inplace zachowują się podobnie jak zwykli operatorzy z wyjątkiem że działają inaczej w przypadku celów zmiennych i niezmiennych. 
 

lista Java jest pusta
  • The _dodać_ metoda wykonuje proste dodawanie, pobiera dwa argumenty, zwraca sumę i przechowuje ją w innej zmiennej bez modyfikowania żadnego z argumentów.
  • Z drugiej strony _add_ metoda również przyjmuje dwa argumenty, ale wprowadza lokalną zmianę w pierwszym przekazanym argumencie, przechowując w nim sumę. Ponieważ w tym procesie wymagana jest mutacja obiektu, cele niezmienne, takie jak ciągi liczbowe i krotki nie powinien mieć metody _iadd_ .
  • „Dodaj ()” normalnego operatorametoda implementuje ' a+b ' i zapisuje wynik we wspomnianej zmiennej.„iadd()” operatora Inplacemetoda implementuje ' a+=b ' jeśli istnieje (tzn. w przypadku celów niezmiennych nie istnieje) i zmienia wartość przekazanego argumentu. Ale jeśli nie, zaimplementowano „a+b”. .


Przypadek 1 : Niezmienne cele.  
W niezmiennych celach, takich jak ciągi liczbowe i krotki. Operatory inplace zachowują się tak samo jak normalne operatory, tj. następuje tylko przypisanie, bez modyfikacji przekazywanych argumentów.
 

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) 

Wyjście:



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


Przypadek 2 : Zmienne cele  
Zachowanie operatorów Inplace w zmiennych obiektach docelowych, takich jak listy i słowniki, różni się od zachowania zwykłych operatorów. The przeprowadzana jest zarówno aktualizacja, jak i przypisanie w przypadku celów zmiennych.
 

Słownik C#
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) 

Wyjście: 
 

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]


 

Utwórz quiz