Оператори на място - Комплект 1 Комплект 2
Нормалните оператори вършат простата работа по присвояване. От друга страна операторите Inplace се държат подобно на нормалните оператори освен че те действат по различен начин в случай на променливи и неизменни цели.
- The _добавяне_ метод прави просто събиране взема два аргумента връща сумата и я съхранява в друга променлива без да променя нито един от аргументите.
- От друга страна _iadd_ Методът също приема два аргумента, но прави промяна на място в 1-ви подаден аргумент, като съхранява сумата в него. Тъй като в този процес е необходима мутация на обекти, неизменни цели като числови низове и кортежи не трябва да има метод _iadd_ .
Случай 1 : Неизменни цели.
В неизменни цели, като числа, низове и кортежи. Операторите на място се държат по същия начин като нормалните оператори, т.е. извършва се само присвояване, без промяна в предадените аргументи.
# 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)
Изход:
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
Случай 2 : Променливи цели
Поведението на операторите Inplace в променливи цели като списъци и речници е различно от нормалните оператори. The актуализацията и присвояването се извършват в случай на променливи цели.
# 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)
Изход:
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]