Python中的就地与标准运算符
就地操作员 – 第 1 组、第 2 组
普通运算符做简单的分配工作。另一方面,Inplace运算符的行为类似于普通运算符,只是它们在可变和不可变目标的情况下以不同的方式运行。
- _add_方法进行简单的加法,接受两个参数,返回总和,并将其存储在另一个变量中,而不修改任何参数。
- 另一方面, _iadd_方法也接受两个参数,但它通过将总和存储在其中来对传递的第一个参数进行就地更改。由于在此过程中需要对象突变,因此不可变目标(例如数字、字符串和元组)不应具有 _iadd_ 方法。
- 普通运算符的“add()”方法,实现“ a+b ”并将结果存储在提到的变量中。
- Inplace 运算符的“iadd()”方法,如果存在“ a+=b ”(即在不可变目标的情况下,它不存在),则实现“a+=b”并更改传递参数的值。但如果不是,则执行“a+b” 。
案例 1 :不可变目标。
在不可变目标中,例如数字、字符串和元组。就地运算符的行为与普通运算符相同,即只进行赋值,不修改传递的参数。
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(a,b)
# using iadd() to add the arguments passed
p = operator.iadd(x,y)
# 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)
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)
输出:
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运算符在可变目标(例如列表和字典)中的行为与普通运算符不同。更新和分配都在可变目标的情况下执行。
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)
输出:
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]