Python 中的 & 符号和竖线字符在哪里?

发布于 2024-11-17 01:41:49 字数 173 浏览 4 评论 0原文

在描述短路评估的维基百科页面中,&| 在 Python 中被列为 eager 运算符。这是什么意思以及它们何时在该语言中使用?

In the Wikipedia page describing short-circuit evaluation, & and | are listed as eager operators in Python. What does this mean and when are they used in the language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白龙吟 2024-11-24 01:41:49

维基百科页面有误,我已更正。 |& 不是布尔运算符,尽管它们是急切运算符,这仅意味着它们不是短路运算符。您可能知道,Python andor 运算符的工作原理如下:

>>> def talk(x):
...     print "Evaluating: ", bool(x)
...     return x
... 
>>> talk(1 == 1) or talk(2 == 1)   # 2 == 1 is not evaluated
Evaluating:  True
True
>>> talk(1 == 1) and talk(2 == 1)
Evaluating:  True
Evaluating:  False
False
>>> talk(1 == 2) and talk(1 == 3)  # 1 == 3 is not evaluated
Evaluating:  False
False

据我所知,Python 没有急切的布尔运算符,它们必须显式编码,例如这样:

>>> def eager_or(a, b):
...    return a or b
...
>>> eager_or(talk(1 == 1), talk(2 == 1))
Evaluating:  True
Evaluating:  False
True

现在,即使 or 仍然短路,调用函数时也会自动计算 ab

至于|&的用法,当与数字一起使用时,它们是二元运算符:

>>> bin(0b11110000 & 0b10101010)
'0b10100000'
>>> bin(0b11110000 | 0b10101010)
'0b11111010'

你最有可能使用|这个使用Python绑定到使用标志的库的方式,例如wxWidgets:

>>> frame = wx.Frame(title="My Frame", style=wx.MAXIMIZE | wx.STAY_ON_TOP)
>>> bin(wx.MAXIMIZE)
'0b10000000000000'
>>> bin(wx.STAY_ON_TOP)
'0b1000000000000000'
>>> bin(wx.MAXIMIZE | wx.STAY_ON_TOP)
'0b1010000000000000'

当与集合一起使用时,它们分别执行交集操作:

>>> set("abcd") & set("cdef")
set(['c', 'd'])
>>> set("abcd") | set("cdef")
set(['a', 'c', 'b', 'e', 'd', 'f'])

The wikipedia page is wrong, I've corrected it. | and & are not boolean operators, even though they are eager operators, which just means that they are not short circuit operators. As you probably know, here's how the python and and or operators work:

>>> def talk(x):
...     print "Evaluating: ", bool(x)
...     return x
... 
>>> talk(1 == 1) or talk(2 == 1)   # 2 == 1 is not evaluated
Evaluating:  True
True
>>> talk(1 == 1) and talk(2 == 1)
Evaluating:  True
Evaluating:  False
False
>>> talk(1 == 2) and talk(1 == 3)  # 1 == 3 is not evaluated
Evaluating:  False
False

As far as I know, python has no eager boolean operators, they would have to be explicitly coded, for instance like this:

>>> def eager_or(a, b):
...    return a or b
...
>>> eager_or(talk(1 == 1), talk(2 == 1))
Evaluating:  True
Evaluating:  False
True

Now a and b are automatically evaluated when the function is called, even though or still short circuits.

As for the usage of | and &, when used with numbers, they are binary operators:

>>> bin(0b11110000 & 0b10101010)
'0b10100000'
>>> bin(0b11110000 | 0b10101010)
'0b11111010'

You're most likely to use | this way with python bindings to libraries that uses flags, like wxWidgets:

>>> frame = wx.Frame(title="My Frame", style=wx.MAXIMIZE | wx.STAY_ON_TOP)
>>> bin(wx.MAXIMIZE)
'0b10000000000000'
>>> bin(wx.STAY_ON_TOP)
'0b1000000000000000'
>>> bin(wx.MAXIMIZE | wx.STAY_ON_TOP)
'0b1010000000000000'

When used with sets, they do the intersection and union operations, respectively:

>>> set("abcd") & set("cdef")
set(['c', 'd'])
>>> set("abcd") | set("cdef")
set(['a', 'c', 'b', 'e', 'd', 'f'])
如果没有 2024-11-24 01:41:49

这里的其他答案缺少的是 &| 在 Python 中没有任何通用含义;它们的含义取决于操作数的类型,使用神奇的 __and____or__ 方法。由于这些是方法,因此操作数在作为参数传递之前都会被评估(即没有短路)。

bool 值上,它们是逻辑“与”和逻辑“或”:

>>> True & False
False
>>> True | False
True
>>> bool.__and__(True, False)
False
>>> bool.__or__(True, False)
True

int 值上,它们是按位“与”和按位“或”:

>>> bin(0b1100 & 0b1010)
'0b1000'
>>> bin(0b1100 | 0b1010)
'0b1110'
>>> bin(int.__and__(0b1100, 0b1010))
'0b1000'
>>> bin(int.__or__(0b1100, 0b1010))
'0b1110'

在集合上,它们是交集和 union:

>>> {1, 2} & {1, 3}
{1}
>>> {1, 2} | {1, 3}
{1, 2, 3}
>>> set.__and__({1, 2}, {1, 3})
{1}
>>> set.__or__({1, 2}, {1, 3})
{1, 2, 3}

一些额外的注意事项:

  • __and____or__ 方法始终在类上查找,而不是在实例上查找。因此,如果您分配 obj.__and__ = lambda x, y: ... 那么它仍然会被调用。
  • 类中的 __rand__ 和 __ror__ 方法将优先(如果已定义)。

有关更多详细信息,请参阅 Python 语言参考

Something missing from the other answers here is that & and | don't have any universal meaning in Python; their meaning depends on the operands' types, using the magic __and__ and __or__ methods. Since these are methods, the operands are both evaluated (i.e. without short-circuiting) before being passed as arguments.

On bool values they are logical "and" and logical "or":

>>> True & False
False
>>> True | False
True
>>> bool.__and__(True, False)
False
>>> bool.__or__(True, False)
True

On int values they are bitwise "and" and bitwise "or":

>>> bin(0b1100 & 0b1010)
'0b1000'
>>> bin(0b1100 | 0b1010)
'0b1110'
>>> bin(int.__and__(0b1100, 0b1010))
'0b1000'
>>> bin(int.__or__(0b1100, 0b1010))
'0b1110'

On sets, they are intersection and union:

>>> {1, 2} & {1, 3}
{1}
>>> {1, 2} | {1, 3}
{1, 2, 3}
>>> set.__and__({1, 2}, {1, 3})
{1}
>>> set.__or__({1, 2}, {1, 3})
{1, 2, 3}

A couple of extra notes:

  • The __and__ and __or__ methods are always looked up on the class, not on the instance. So if you assign obj.__and__ = lambda x, y: ... then it's still obj.__class__.__and__ that's invoked.
  • The __rand__ and __ror__ methods on the class will take priority, if they are defined.

See the Python language reference for more details.

朕就是辣么酷 2024-11-24 01:41:49

这意味着始终评估左操作数和右操作数。 & 是按位 AND 运算符,| 是按位 OR 运算符。

It means the left operand and the right operand are always evaluated. & is the bitwise AND operator and | is the bitwise OR operator.

污味仙女 2024-11-24 01:41:49

& ---->用于按位与,即逐位

类似地,

| --->习惯 OR 按位

当你发现任何 python 问题时,尝试使用 python.org,它很有帮助

& ----> Used to AND bitwise i.e. bit by bit

similarly,

| ---> used to OR bitwise

Whenevr you find any python problem, try to use python.org, its helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文