How are Python in-place operator functions different than the standard operator functions?
为什么
从文档中:
Many operations have an"in-place"
version. The following functions
provide a more primitive access to
in-place operators than the usual
syntax does; for example, the
statement x += y is equivalent to x =
operator.iadd(x, y). Another way to
put it is to say that z =
operator.iadd(x, y) is equivalent to
the compound statement z = x; z += y.
号
相关问题,但我对Python类方法不感兴趣;只对内置的Python类型使用常规运算符。
首先,您需要了解
对象的
一个对象的
另一个问题是:
你可以很容易地看到这一点:
1 2 3 4 5 6 7 8 | import operator a = 1 operator.iadd(a, 2) # a is still 1, because ints don't have __iadd__; iadd returned 3 b = ['a'] operator.iadd(b, ['b']) # lists do have __iadd__, so b is now ['a', 'b'] |
可能是因为一些Python对象是不可变的。
我猜