|= (ior) 在 Python 中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3929278/
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
What does |= (ior) do in Python?
提问by Sean W.
Google won't let me search |=so I'm having trouble finding relevant documentation. Anybody know?
Google 不允许我进行搜索,|=因此我无法找到相关文档。有人知道吗?
采纳答案by Daniel Stutzbach
In Python, and many other programming languages, |is the bitwise-OR operation. |=is to |as +=is to +, i.e. a combination of operation and asignment.
在 Python 和许多其他编程语言中,|是按位或运算。 |=is to |as +=is to +,即操作和赋值的结合。
var |= valueis short for var = var | value
var |= value是简称 var = var | value
回答by Brandon Rhodes
It performs a binary bitwise OR of the left-hand and right-hand sides of the assignment, then stores the result in the left-hand variable.
它对赋值的左侧和右侧执行二进制按位或,然后将结果存储在左侧变量中。
http://docs.python.org/reference/expressions.html#binary-bitwise-operations
http://docs.python.org/reference/expressions.html#binary-bitwise-operations
回答by fedorqui 'SO stop harming'
This is just an OR operation between the current variable and the other one. Being T=Trueand F=False, see the output graphically:
这只是当前变量和另一个变量之间的 OR 运算。作为T=True和F=False,以图形方式查看输出:
r s r|=s
--------------
T T T
T F T
F T T
F F F
For example:
例如:
>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True
回答by sunny
When used with sets it performs union operation.
当与集合一起使用时,它执行联合操作。
回答by pylang
|=performs an in-place+operation between pairs of objects. In particular, between:
|=在对象对之间执行就地+操作。特别是,在:
In most cases, it is related to the |operator. See examples below.
在大多数情况下,它与|运营商有关。请参阅下面的示例。
Sets
套
For example, the union of two sets s1and s2share the following equivalent expressions:
例如,两个集合的s1并集和s2共享以下等价表达式:
>>> s1 = s1 | s12 # 1
>>> s1 |= s2 # 2
>>> s1.__ior__(s2) # 3
where the final value of s1is equivalent either by:
其中 的最终值s1等于:
- an assigned OR operation
- an in-place OR operation
- an in-place OR operation via special method++
- 分配的 OR 操作
- 就地 OR 操作
- 通过特殊方法++的就地OR操作
Example
例子
Here we apply OR (|) and the inplace OR (|=) to sets:
这里我们将 OR ( |) 和就地 OR ( |=) 应用于集合:
>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}
>>> # OR, |
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1 # `s1` is unchanged
{'a', 'b', 'c'}
>>> # Inplace OR, |=
>>> s1 |= s2
>>> s1 # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}
Dictionaries
字典
In Python 3.9+, new merge (|) and update (|=) operators are proposed between dictionaries. Note: these are not the same as set operators mentioned above.
在Python 3.9+ 中,字典之间提出了新的合并 ( |) 和更新 ( |=) 运算符。注意:这些与上面提到的集合运算符不同。
Given operations between two dicts d1and d2:
给定两个 dictd1和之间的操作d2:
>>> d1 = d1 | d2 # 1
>>> d1 |= d2 # 2
where d1is equivalent via:
其中d1等效于:
- an assigned merge-right operation
- an in-place merge-right (update) operation; equivalent to
d1.update(d2)
- 分配的合并权操作
- 就地合并权限(更新)操作;相当于
d1.update(d2)
Example
例子
Here we apply merge (|) and update (|=) to dicts:
这里我们将merge( |)和update( |=)应用于dicts:
>>> d1 = {"a": 0, "b": 1, "c": 2}
>>> d2 = {"c": 20, "d": 30}
>>> # Merge, |
>>> d1 | d2
{"a": 0, "b": 1, "c": 20, "d": 30}
>>> d1
{"a": 0, "b": 1, "c": 2}
>>> # Update, |=
>>> d1 |= d2
>>> d1
{"a": 0, "b": 1, "c": 20, "d": 30}
Numbers
数字
Lastly, you can do binary math.
最后,您可以进行二进制数学运算。
Given operations between two numbers n1and n2:
给定两个数字n1和之间的运算n2:
>>> n1 = n1 | n2 # 1
>>> n1 |= n2 # 2
where n1is equivalent via:
其中n1等效于:
- an assigned bitwise OR operation
- an in-place bitwise OR operation
- 分配的按位 OR 运算
- 就地按位或运算
Example
例子
Here we apply bitwise OR (|) and the in-place bitwise OR (|=) to numbers:
这里我们将按位 OR ( |) 和就地按位 OR ( |=) 应用于数字:
>>> n1 = 0
>>> n2 = 1
>>> # Bitwise OR, |
>>> n1 | n2
1
>>> n1
0
>>> # Inplace Bitwise OR, |=
>>> n1 |= n2
1
>>> n1
1
Review
This section briefly reviews some bitwise math. In the simplest case, the bitwise OR operation compares two binary bits. It will always return 1except when both bits are 0.
本节简要回顾一些按位数学。在最简单的情况下,按位或运算比较两个二进制位。1除非两个位都为 ,否则它将始终返回0。
>>> assert 1 == (1 | 1) == (1 | 0) == (0 | 1)
>>> assert 0 == (0 | 0)
We now extend this idea beyond binary numbers. Given any two integral numbers (lacking fractional components), we apply the bitwise OR and get an integral result:
我们现在将这个想法扩展到二进制数之外。给定任意两个整数(缺少小数部分),我们应用按位 OR 并得到一个整数结果:
>>> a = 10
>>> b = 16
>>> a | b
26
How? In general, the bitwise operations follow some "rules":
如何?一般来说,按位运算遵循一些“规则”:
- internally compare binary equivalents
- apply the operation
- return the result as the given type
- 内部比较二进制等价物
- 应用操作
- 将结果作为给定类型返回
Let's apply these rules to our regular integers above.
让我们将这些规则应用于上面的常规整数。
(1) Compare binary equivalents, seen here as strings (0bdenotes binary):
(1) 比较二进制等价物,此处视为字符串(0b表示二进制):
>>> bin(a)
'0b1010'
>>> bin(b)
'0b10000'
(2) Apply a bitwise OR operation to each column (0when both are 0, else 1):
(2) 对每一列应用按位 OR 运算(0当两者都是0,else 时1):
01010
10000
-----
11010
(3) Return the result in the given type, e.g. base 10, decimal:
(3) 返回给定类型的结果,例如基数为 10,十进制:
>>> int(0b11010)
26
The internal binary comparison means we can apply the latter to integers in any base, e.g. hex and octal:
内部二进制比较意味着我们可以将后者应用于任何基数的整数,例如十六进制和八进制:
>>> c = 0xa
>>> d = 0o32
>>> c | d
26
See Also
也可以看看
- An example of overloading the
__ior__()methodto iterate iterables in aMutableSetabstract base class - R. Hettinger's OrderedSet recipe (see lines 3 and 10 respectively)
- A thread on Python-ideason why to use
|=to update a set - A section B.8 of Dive in Python 3on special methods of Python operators
- 重载
__ior__()方法以迭代MutableSet抽象基类中的可迭代对象的示例 - R. Hettinger 的OrderedSet 配方(分别参见第 3 行和第 10 行)
- 一对Python的想法线程上为什么要使用
|=更新一组 - 一个在Python 3潜水的部分B.8Python的运营商的特殊方法
+The in-place bitwise OR operator cannot be applied to literals; assign objects to names.
+就地按位 OR 运算符不能应用于文字;将对象分配给名称。
++Special methods return the same operations as their corresponding operators.
++特殊方法返回与其对应运算符相同的操作。
回答by memeKing
It's bitwise or.
Let's say we have 32 |= 10, picture 32 and 10 is binary.
它是按位或。假设我们有32 |= 10,图片 32 和 10 是二进制的。
32 = 10 0000
10 = 00 1010
Now because | is or, do a bitwise or on the two numbers
现在因为| 是或,对两个数字做按位或
i.e 1 or 0 --> 1, 0 or 0 --> 0. Continue this down the chain
即 1 或 0 --> 1、0 或 0 --> 0。沿链继续下去
10 0000 | 00 1010 = 10 1010.
Now change the binary into a decimal, 10 1010 = 42.
现在把二进制变成十进制,10 1010 = 42。
For |=, think of the known examples, x +=5. It means x = x + 5,therefore if we have x |= 5, it means x = x bitwiseor with 5.
对于 |=,想想已知的例子,x +=5。这意味着x = x + 5,因此,如果我们有x |= 5,它的意思x = x bitwiseor with 5。
回答by aanchal.s
In Python,|=(ior) works like union operation. like if x=5 and x|=5 then both the value will first convert in binary value then the union operation will perform and we get the answer 5.
在 Python 中,|=(ior) 的工作方式类似于联合操作。就像如果 x=5 和 x|=5 那么这两个值将首先转换为二进制值,然后联合操作将执行,我们得到答案 5。
回答by scharfmn
To give a use-case (after spending time with the other answers):
给出一个用例(在花时间回答其他答案之后):
def process(item):
return bool(item) # imagine some sort of complex processing taking place above
def any_success(data): # return True if at least one is successful
at_least_one = False
for item in data:
at_least_one |= process(item)
return at_least_one
>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True
Basically anywithout the short-circuiting: might be useful if you need to process every item and record at least one success etc.
基本上any没有短路:如果您需要处理每个项目并记录至少一个成功等,可能会很有用。
See also the caveats in this answer
另请参阅此答案中的警告

