logo

Operadores in situ vs estándar en Python

Operadores In situ - Conjunto 1 Conjunto 2
Los operadores normales hacen el trabajo de asignación simple. Por otro lado, los operadores Inplace se comportan de manera similar a los operadores normales. excepto que actúan de manera diferente en caso de objetivos mutables e inmutables. 
 

  • El _agregar_ El método hace una suma simple, toma dos argumentos, devuelve la suma y la almacena en otra variable sin modificar ninguno de los argumentos.
  • Por otro lado _iadd_ El método también toma dos argumentos, pero realiza un cambio in situ en el primer argumento pasado al almacenar la suma en él. Como se necesita la mutación de objetos en este proceso, objetivos inmutables como cadenas de números y tuplas no debería tener el método _iadd_ .
  • 'add()' del operador normalmétodo implementa ' a+b ' y almacena el resultado en la variable mencionada.'iadd()' del operador in situmétodo implementa ' a+=b ' si existe (es decir, en el caso de objetivos inmutables, no existe) y cambia el valor del argumento pasado. Pero si no se implementa 'a+b' .


Caso 1 : Objetivos inmutables.  
En objetivos inmutables, como cadenas de números y tuplas. Los operadores in situ se comportan igual que los operadores normales, es decir, solo se realiza la asignación, no se realizan modificaciones en los argumentos pasados.
 

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) 

Producción:



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


Caso 2 : Objetivos mutables  
El comportamiento de los operadores Inplace en objetivos mutables como listas y diccionarios es diferente del de los operadores normales. El tanto la actualización como la asignación se llevan a cabo en el caso de objetivos mutables.
 

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) 

Producción: 
 

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]


 

Crear cuestionario