Python 中的管道字符
我看到函数调用中使用了“管道”字符 (|
):
res = c1.create(go, come, swim, "", startTime, endTime, "OK", ax|bx)
ax|bx
中管道的含义是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这也是并集运算符,
这将导致
set([1, 2, 3])
This is also the union set operator
This will result in
set([1, 2, 3])
它是整数的按位或。例如,如果
ax
或bx
之一或两者为1
,则计算结果为1
,否则为 <代码>0。它也适用于其他整数,例如15 | 128 = 143,即
00001111 |二进制形式为 10000000 = 10001111
。It is a bitwise OR of integers. For example, if one or both of
ax
orbx
are1
, this evaluates to1
, otherwise to0
. It also works on other integers, for example15 | 128 = 143
, i.e.00001111 | 10000000 = 10001111
in binary.在 Python 3.9 - PEP 584 - 将联合运算符添加到字典 的标题为的部分中规范,对运算符进行了解释。
管道被增强以合并(联合)字典。
注释 1 如果一个键同时出现在两个操作数中,则最后看到的值(即来自右侧操作数的值)获胜 --> 'cheese': 4 而不是 'cheese': 3
comment 2 奶酪出现两次,选择第二个值,因此
d[cheese]=3
In Python 3.9 - PEP 584 - Add Union Operators To dict in the section titled Specification, the operator is explained.
The pipe was enhanced to merge (union) dictionaries.
comment 1 If a key appears in both operands, the last-seen value (i.e. that from the right-hand operand) wins --> 'cheese': 4 instead of 'cheese': 3
comment 2 cheese appears twice, the second value is selected so
d[cheese]=3
是的,以上所有答案都是正确的。
尽管您可以找到“|”的更多奇特用例,但如果它是类使用的重载运算符,例如
https://github.com/twitter/pycascading/wiki#pycascading
在此特定用例中管道“|”可以更好地将运算符视为 Unix 管道运算符。但我同意,按位运算符和并集运算符是“|”更常见的用例在Python中。
Yep, all answers above are correct.
Although you could find more exotic use cases for "|", if it is an overloaded operator used by a class, for example,
https://github.com/twitter/pycascading/wiki#pycascading
In this specific use case pipe "|" operator can be better thought as a unix pipe operator. But I agree, bit-wise operator and union set operator are much more common use cases for "|" in Python.
总结与扩展以前的答案:
|
运算符最初的作用是 按位或。这是按位运算教程。另一方面,Python 运算符可以通过重载其等效的运算符函数来重载。特别是,可以通过实现函数
__or__
(和__ror__
)。任何 Python 库,无论是标准库还是用户库,都可以重载任何运算符(例如,存档的 PyCascading )。Python 内置类型,例如
set
和dict
重载 < code>| 分别定义联合和合并。特别是,Python 3.9、PEP 584,将重载的|
和其他运算符引入到dict
和其他标准库类型。同样,
types.UnionType
< /strong>,随 Python 3.10 引入,还重载了二元或运算符|
,在本例中表示 types 用于类型注释/提示,例如,typing.Union[int, str] == int | str。最近流行的
|
运算符的重载和使用位于 Python LangChain 库到链/管道操作。这是 LangChain 如何重载算子的具体代码。Summarizing & extending previous answers:
The
|
operator originally does bitwise OR. Here is a tutorial on bitwise operations.On the other hand, Python operators can be overloaded by overloading their equivalent operator functions. In particular,
|
can be overloaded by implementing the function__or__
(and__ror__
). Any Python library, standard or userbase, can overload any operator (e.g., archived PyCascading).The Python built-in types such as
set
anddict
overload|
to define union and merge, respectively. In particular, Python 3.9, PEP 584, brought the overloaded|
and other operators todict
and other standard library types.Likewise, the
types.UnionType
, introduced with Python 3.10, also overloads the binary-or operator|
, in this case to signify a union of types for type annotations/hints, e.g.,typing.Union[int, str] == int | str
.A recent, popular overloading and usage of the
|
operator is in the Python LangChain library to chain/pipe operations. This is the exact code for how LangChain overloads the operator.按位或。
Bitwise OR.
它是按位或。
Python 中所有运算符的文档可以在 Python 文档的 索引 - 符号 页面中找到。
It is a bitwise-or.
The documentation for all operators in Python can be found in the Index - Symbols page of the Python documentation.
不是专门在函数调用中,而是关于 OP 标题,现在人们可能还会在键入注释中列出“或”功能的管道运算符!
请参阅: https://docs.python.org/3/library/ Typing.html#typing.Union
例如:
Not specifically in function call but regarding OPs headline one might nowadays also list the pipe operator for "or" functionality in typing annotations!
See: https://docs.python.org/3/library/typing.html#typing.Union
For example:
从 python 3.10 开始,您还可以使用管道运算符 (
|
) 来匹配匹配语句中的多种情况(结构模式匹配),例如:
https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching
Since python 3.10 you can also use the pipe operator (
|
) to match several cases inside the match statement (Structural Pattern Matching)Such as in:
https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching