Opérateurs sur place - Ensemble 1 Ensemble 2
Les opérateurs normaux effectuent le simple travail d’attribution. D'un autre côté, les opérateurs Inplace se comportent de la même manière que les opérateurs normaux. sauf qu'ils agissent de manière différente en cas de cibles mutables et immuables.
dérivés partiels en latex
- Le _ajouter_ La méthode effectue une addition simple, prend deux arguments, renvoie la somme et la stocke dans une autre variable sans modifier aucun des arguments.
- D'autre part _iadd_ La méthode prend également deux arguments mais elle modifie sur place le premier argument passé en y stockant la somme. Comme une mutation d'objet est nécessaire dans ce processus, des cibles immuables telles que des chaînes de nombres et des tuples ne devrait pas avoir la méthode _iadd_ .
Cas 1 : Cibles immuables.
Dans les cibles immuables telles que les chaînes de nombres et les tuples. Les opérateurs sur place se comportent de la même manière que les opérateurs normaux, c'est-à-dire que seule l'affectation a lieu, aucune modification n'a lieu dans les arguments passés.
# 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)
Sortir:
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
Cas 2 : Cibles mutables
Le comportement des opérateurs Inplace dans les cibles mutables telles que les listes et les dictionnaires est différent de celui des opérateurs normaux. Le la mise à jour et l'affectation sont toutes deux effectuées en cas de cibles mutables.
chaîne en javaPython
# 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)
Sortir:
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]