Operadores locais - Conjunto 1 Conjunto 2
Os operadores normais fazem o trabalho de atribuição simples. Por outro lado, os operadores Inplace se comportam de forma semelhante aos operadores normais exceto que atuem de maneira diferente no caso de alvos mutáveis e imutáveis.
- O _adicionar_ O método faz adição simples, pega dois argumentos, retorna a soma e a armazena em outra variável sem modificar nenhum dos argumentos.
- Por outro lado _iad_ O método também aceita dois argumentos, mas faz uma alteração no local no primeiro argumento passado, armazenando a soma nele. Como a mutação do objeto é necessária neste processo, alvos imutáveis, como sequências de números e tuplas não deveria ter o método _iadd_ .
Caso 1 : Alvos imutáveis.
Em alvos imutáveis, como sequências de números e tuplas. Os operadores locais se comportam da mesma forma que os operadores normais, ou seja, apenas a atribuição ocorre, nenhuma modificação ocorre nos argumentos passados.
# 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)
Saída:
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 : Alvos Mutáveis
O comportamento dos operadores Inplace em destinos mutáveis, como listas e dicionários, é diferente dos operadores normais. O atualização e atribuição são realizadas no caso de alvos mutáveis.
# 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)
Saída:
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]