English 中文(简体)
干 the的经营者如何不同于标准操作者的职能?
原标题:How are Python in-place operator functions different than the standard operator functions?

http://docs.python.org/library/operator.html

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.

问题:

  • 为什么t operator.iadd(x, y)z = x;z += y?

  • operator.iadd(x, y)不同于operator.add(x, y)?

Relatd question,但I m不关心甲型类避孕方法;仅是固定操作者。

最佳回答

首先,你们需要理解<代码>__add__和__iadd__之间的区别。

物体add__方法为定期添加:它有两个参数,收回其数额,并且不修改任何参数。

物体__iadd__方法也具有两个参数,但修改了第一个参数的内容。 由于这要求的是标的突变,不可改变的类型(如标准编号类型)的tt有方法。

add__。 如果存在,则通过<代码>add__>/code>予以推广,如tmp = a + b; a = tmp,operator.addoperator.iadd

其他问题:operator.iadd(x, y) isn tequi to z = x;z += y ,因为如果没有iadd__,将使用add__。 你们需要分配价值,以确保结果储存在两种情况下:x = 操作者.iadd(x, y)

你们可以很容易地看到:

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 ]
问题回答

或许是因为一些灰色物体不可变。

I m guessing operator.iadd(x, y)相当于z = x;z += y只适用于诸如字典和清单等变式类型,但不适用于像数字和方言等不可改变的类型。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签