Python:*= 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20622890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:51:14  来源:igfitidea点击:

Python: What does *= mean?

python

提问by k_rose

In Python, what does it mean when *=is used. For example:

在 Python 中,*=使用when是什么意思。例如:

for i in xrange(len(files)):
    itimes[i,:,:] *= thishdr["truitime"]

回答by Hyman

It just means "[expression on the left] = [itself] * [expression on the right]":

它只是意味着“[左边的表达式] = [本身] * [右边的表达式]”:

itimes[i,:,:] *= thishdr["truitime"]

itimes[i,:,:] *= thishdr["truitime"]

is equivalent to

相当于

itimes[i,:,:] = itimes[i,:,:] * thishdr["truitime"]

itimes[i,:,:] = itimes[i,:,:] * thishdr["truitime"]

回答by theodox

It means "set this variable to itself times "

这意味着“将此变量设置为自身次数”

>>> fred = 10
>>> fred *= 10                                                                                                                                                                                                                              
>>> fred                                                                                                                                                                                                                                    
100                                                                                                                                                                                                                                         
>>> barney = ["a"]                                                                                                                                                                                                                            
>>> barney *= 10                                                                                                                                                                                                                              
>>> barney 
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']                                                                                                                                                                                          

回答by aIKid

As others have explained, this is roughly an equivalent to:

正如其他人所解释的,这大致相当于:

[object] = [object] * [another_object]

However, it's not exactly the same. Technically, the above calls the __mul__function, which returns a value, and reassign it back to the name.

然而,它并不完全相同。从技术上讲,上面调用了__mul__返回一个值的函数,并将其重新分配给名称。

For example, we have an object Aand multiplying it with B. The process is something like this:

例如,我们有一个对象A并将其与 相乘B。这个过程是这样的:

> Call the __mul__ function of object A, 
> Retrieve the new object returned.
> Reassign it to the name A.

Looks simple. Now, by doing *=we're not invoking the method __mul__, but instead __imul__, which will attempt to modify itself. The process is something like this:

看起来很简单。现在,通过这样做,*=我们不是在调用方法__mul__,而是__imul__,它将尝试修改自身。这个过程是这样的:

> Call the __imul__ function of object A,
> __imul__ will change the value of the object, it returns the modified object itself
> The value is reassigned back to the name A, but still points to the same place in memory.

With this, you're modifying it in-place, not creating a new object.

有了这个,你就地修改它,而不是创建一个新对象。

So what? It looks the same..

所以呢?看起来一样。。

Not exactly. If you replaces an object, you created a new place for it in memory. If you modify it in-place, the object location in the memory will always be the same.

不完全是。如果你替换了一个对象,你就在内存中为它创建了一个新位置。如果就地修改它,内存中的对象位置将始终相同。

Take a look at this console session:

看看这个控制台会话:

>>> a = [1, 2, 3]
>>> b = a
>>> c = 10
>>> a = a * c
>>> print a
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> b
[1, 2, 3]

If we check the memory address:

如果我们检查内存地址:

>>> id(a) == id(b)

False

错误的

Using this, the value of bis unchanged, since ais now just pointing to a different place. But using *=:

使用这个,值b不变,因为a现在只是指向不同的地方。但是使用*=

>>> a = [1, 2, 3]
>>> b = a
>>> c = 10
>>> a *= c
>>> b
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

And if we check the memory address:

如果我们检查内存地址:

>>> id(a) == id(b)
True

The operation affects bas well. This can be tricky and leading to confusing behaviours, sometimes. But once you understand it, it would be easy to handle.

操作也有影响b。这可能很棘手,有时会导致令人困惑的行为。但是一旦你理解了它,它就会很容易处理。

Hope this helps!

希望这可以帮助!