为什么Python中没有“和”和“或”运算符?

时间:2022-06-16 14:01:14

I wasn't aware of this, but apparently the and and or keywords aren't operators. They don't appear in the list of python operators. Just out of sheer curiosity, why is this? And if they aren't operators, what exactly are they?

我没有意识到这一点,但显然和和或关键字不是运营商。它们不会出现在python运算符列表中。出于纯粹的好奇心,这是为什么?如果他们不是运营商,他们究竟是什么?

5 个解决方案

#1


44  

Because they're control flow constructs. Specifically:

因为它们是控制流构造。特别:

  • if the left argument to and evaluates to False, the right argument doesn't get evaluated at all
  • 如果左参数为to并且计算结果为False,则右参数根本不会被评估

  • if the left argument to or evaluates to True, the right argument doesn't get evaluated at all
  • 如果左参数或评估为True,则根本不会评估正确的参数

Thus, it is not simply a matter of being reserved words. They don't behave like operators, since operators always evaluate all of their arguments.

因此,这不仅仅是保留词的问题。它们的行为与运算符不同,因为运算符总是会评估它们的所有参数。

You can contrast this with bitwise binary operators which, as the name implies, are operators:

你可以将它与按位二元运算符进行对比,顾名思义,它是运算符:

>>> 1 | (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 1 or (1/0)
1

As you see, the bitwise OR (|) evaluates both its arguments. The or keyword, however, doesn't evaluate its right argument at all when the left argument evaluates to True; that's why no ZeroDivisionError is raised in the second statement.

如您所见,按位OR(|)计算其参数。但是,当左参数的计算结果为True时,or关键字根本不会评估其正确的参数。这就是为什么在第二个语句中没有引发ZeroDivisionError的原因。

#2


6  

Python does not currently provide any 'xxx' special methods corresponding to the 'and', 'or' and 'not' boolean operators. In the case of 'and' and 'or', the most likely reason is that these operators have short-circuiting semantics, i.e. the second operand is not evaluated if the result can be determined from the first operand. The usual technique of providing special methods for these operators therefore would not work.

Python目前不提供与'and','或'和'not'布尔运算符对应的任何'xxx'特殊方法。在'和'和'或'的情况下,最可能的原因是这些运算符具有短路语义,即如果可以从第一操作数确定结果则不评估第二操作数。因此,为这些操作员提供特殊方法的常用技术是行不通的。

Source: PEP 335

资料来源:PEP 335

PEP 335 talks about adding the ability to have overloadable operators, and discusses this issue a bit.

PEP 335讨论了增加具有可重载运算符的能力,并讨论了这个问题。

#3


0  

They're classifying them as keywords earlier in the document.

他们在文档的前面将它们分类为关键字。

#4


0  

They're keywords, because they're reserved identifiers, not special tokens of symbols.

它们是关键字,因为它们是保留的标识符,而不是特殊的符号标记。

#5


0  

They can't be redefined to support type-specific operations, so they don't fall under the scope of the other operators.

它们不能重新定义以支持特定于类型的操作,因此它们不属于其他运算符的范围。

#1


44  

Because they're control flow constructs. Specifically:

因为它们是控制流构造。特别:

  • if the left argument to and evaluates to False, the right argument doesn't get evaluated at all
  • 如果左参数为to并且计算结果为False,则右参数根本不会被评估

  • if the left argument to or evaluates to True, the right argument doesn't get evaluated at all
  • 如果左参数或评估为True,则根本不会评估正确的参数

Thus, it is not simply a matter of being reserved words. They don't behave like operators, since operators always evaluate all of their arguments.

因此,这不仅仅是保留词的问题。它们的行为与运算符不同,因为运算符总是会评估它们的所有参数。

You can contrast this with bitwise binary operators which, as the name implies, are operators:

你可以将它与按位二元运算符进行对比,顾名思义,它是运算符:

>>> 1 | (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 1 or (1/0)
1

As you see, the bitwise OR (|) evaluates both its arguments. The or keyword, however, doesn't evaluate its right argument at all when the left argument evaluates to True; that's why no ZeroDivisionError is raised in the second statement.

如您所见,按位OR(|)计算其参数。但是,当左参数的计算结果为True时,or关键字根本不会评估其正确的参数。这就是为什么在第二个语句中没有引发ZeroDivisionError的原因。

#2


6  

Python does not currently provide any 'xxx' special methods corresponding to the 'and', 'or' and 'not' boolean operators. In the case of 'and' and 'or', the most likely reason is that these operators have short-circuiting semantics, i.e. the second operand is not evaluated if the result can be determined from the first operand. The usual technique of providing special methods for these operators therefore would not work.

Python目前不提供与'and','或'和'not'布尔运算符对应的任何'xxx'特殊方法。在'和'和'或'的情况下,最可能的原因是这些运算符具有短路语义,即如果可以从第一操作数确定结果则不评估第二操作数。因此,为这些操作员提供特殊方法的常用技术是行不通的。

Source: PEP 335

资料来源:PEP 335

PEP 335 talks about adding the ability to have overloadable operators, and discusses this issue a bit.

PEP 335讨论了增加具有可重载运算符的能力,并讨论了这个问题。

#3


0  

They're classifying them as keywords earlier in the document.

他们在文档的前面将它们分类为关键字。

#4


0  

They're keywords, because they're reserved identifiers, not special tokens of symbols.

它们是关键字,因为它们是保留的标识符,而不是特殊的符号标记。

#5


0  

They can't be redefined to support type-specific operations, so they don't fall under the scope of the other operators.

它们不能重新定义以支持特定于类型的操作,因此它们不属于其他运算符的范围。