热线电话:13121318867

登录
2019-03-30 阅读量: 479
什么是可变目标

可变目标

Inplace运算符在可变目标(例如列表和字典)中的行为与普通运算符不同。更新和赋值都是在可变目标的情况下执行的。

# 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]
0.0000
5
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子