Python a &= b 是什么意思? [英] Python a &= b meaning?

查看:87
本文介绍了Python a &= b 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

&= 运算符在 Python 中是什么意思,你能给我一个可行的例子吗?

我试图理解 __iand__运算符.

我只是不知道 &= 是什么意思,我在网上找过但找不到.

解决方案

说明

可以理解的是,您找不到太多参考资料.我发现很难得到这方面的参考也是,但它们存在.

iand 中的 i 表示 in-place,所以它是 & 的就地运算符.&= 调用 __iand__ 运算符(如果已实现).如果没有实现,则同 x = x &y.

内置示例,集合:

主要用于更新内置集合类型的交集:

<预><代码>>>>a = set('abc')>>>a &= set('cbe')>>>一种设置(['c','b'])

等同于:

<预><代码>>>>a = set('abc')>>>a.__ianand__(set('cbe'))设置(['c','b'])

它与调用 set.intersection_update 方法非常相似,并且会在控制流中使用,因为您将对任何对象或变量进行就地更新(如果对象是不可变的).

未实现的内置示例

不常用的不可变的frozenset对象会在就地更新时在内存中被替换,变量名将指向内存中的新对象.

<预><代码>>>>a =frozenset('abc')>>>a &= set('bce')>>>一种冻结集({'c','b'})

在这种情况下,因为frozenset没有实现__iand__方法,

<预><代码>>>>a =frozenset('abc')>>>a.__ianand__(set('cbe'))回溯(最近一次调用最后一次):文件<pyshell#160>",第 1 行,在 <module> 中a =frozenset('abc');a.__ianand__(set('cbe'))AttributeError: 'frozenset' 对象没有属性 '__iand__'

(*几乎)相同

a = a &设置('bce')

*(我说几乎是因为如果你检查字节码,你会发现底层实现对集合和冻结集的处理是一样的,即使冻结集没有 __iand__ 和 set 做,因为每个调用 INPLACE_AND,至少对于已编译的函数.)

内置示例,二进制标志:

与 Sets 类似,我们可以使用 &= 来更新二进制选项标志的交集,其中 True 的值为 1.下面,我们演示了二进制的binary AND"(类似于交集)数字 111010111010:

<预><代码>>>>option_flags = int('1110', 2)>>>option_flags14>>>option_flags &= int('1011', 2)>>>option_flags10>>>bin(option_flags)'0b1010'

由于 int 对象不是可变的,就像 frozenset 示例一样,这实际上只是将变量 option_flags 重新分配给新计算的值.

What does the &= operator mean in Python, and can you give me a working example?

I am trying to understand the __iand__ operator.

I just don't know what &= means and have looked online but couldn't find it.

解决方案

Explanation

Understandable that you can't find much reference on it. I find it hard to get references on this too, but they exist.

The i in iand means in-place, so it's the in-place operator for &. &= calls the __iand__ operator, if it is implemented. If not implemented, it is the same as x = x & y.

Built-in Example, Sets:

It's primarily used to update the intersection of built-in set types:

>>> a = set('abc') 
>>> a &= set('cbe')
>>> a
set(['c', 'b'])

which is the same as:

>>> a = set('abc')
>>> a.__iand__(set('cbe'))
set(['c', 'b'])

It is very similar to calling the set.intersection_update method, and would be used in control flow as you would do an in-place update of any object or variable (if the object is immutable).

Unimplemented Built-in Example

The less commonly used immutable frozenset object would be replaced in memory on the inplace update, and the variable name would point to the new object in memory.

>>> a = frozenset('abc')
>>> a &= set('bce')
>>> a
frozenset({'c', 'b'})

In this case, since frozenset doesn't implement an __iand__ method,

>>> a = frozenset('abc')
>>> a.__iand__(set('cbe'))

Traceback (most recent call last):
  File "<pyshell#160>", line 1, in <module>
    a = frozenset('abc'); a.__iand__(set('cbe'))
AttributeError: 'frozenset' object has no attribute '__iand__'

it is (*nearly) identical to

a = a & set('bce')

*(I say nearly because if you examine the bytecode, you'll see that the underlying implementation treats sets and frozensets the same, even though frozensets don't have __iand__, and sets do, because each calls INPLACE_AND, at least for compiled functions.)

Built-in Example, Binary Flags:

Similar to Sets, we can use the &= to update the intersection of binary option flags where the value for True is 1. Below, we demonstrate that the "binary AND", (akin to intersection) of the binary numbers 1110 and 1011 is 1010:

>>> option_flags = int('1110', 2)
>>> option_flags
14
>>> option_flags &= int('1011', 2)
>>> option_flags
10
>>> bin(option_flags)
'0b1010'

Since int objects are not mutable, like the frozenset example, this actually only reassigns the variable option_flags to the newly calculated value.

这篇关于Python a &amp;= b 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆