简体   繁体   English

Python布尔值评估顺序

[英]Python boolean evaluation order

I'm working with linked lists in Python and couldn't quite figure out the error statement it was putting out. 我正在使用Python中的链接列表,无法完全弄清它所发出的错误声明。

This is my code to increment through the nodes from the head of the list: 这是我的代码,用于从列表的头开始遍历节点:

    while current.next_node is not None and value > current.next_node.data:
        current = current.next_node
    current.next_node = Node(value, current.next_node)

While the above works, this doesn't: 尽管上述方法有效,但不能:

    while value > current.next_node.data and current.next_node is not None:
        current = current.next_node
    current.next_node = Node(value, current.next_node)

I figured that as long as the while statement evaluated to either true or false, that it would behave accordingly. 我认为,只要while语句评估为true或false,它就会相应地运行。 Instead, it throws an error when it tries to run value > current.next_node.data alone and realizes that the current.next_node doesn't exist. 相反,当尝试单独运行value > current.next_node.data并意识到current.next_node不存在时,它将引发错误。

Why is this? 为什么是这样?

Arguments evaluate left to right and there is a "principle of economy". 参数从左到右评估,并且有一个“经济原理”。

  • In an "and" clause, the first that gives a False value stops the evaluation and returns False. 在“和”子句中,第一个给出False值的子句将停止求值并返回False。

  • In an "or" clause, the First that returns True stops the evaluation and returns True. 在“或”子句中,返回True的First停止求值并返回True。

You can use it in your favor, by first evaluating the "safety" condition and then the other. 您可以通过先评估“安全”条件然后再评估其他条件来使用它,以对您有利。

while current.next_node is not None and value > current.next_node.data:
    current = current.next_node
current.next_node = Node(value, current.next_node)

In this way, the failure of the first condition will always stop the evaluation. 这样,第一个条件的失败将始终停止评估。 And if it returns true, then the second will execute correctly. 如果返回true,则第二个将正确执行。

In the other version, you may be trying to access current.next_node.data without the safety. 在另一个版本中,您可能试图在没有安全性的current.next_node.data下访问current.next_node.data And if current is None, it will trigger an exception. 如果current为None,它将触发异常。

Python does short circuit evaluation which is why you're seeing the behavior you are. Python进行短路评估 ,这就是为什么您看到自己的行为的原因。 This comes into play in and and or clauses similarly. 这在andor子句中同样起作用。

With and clauses, if the expression on the left side evaluates to False there is no way the entire statement can evaluate to True (False and anything is False) so it doesn't bother to evaluate the expression on the right-hand side. 使用and子句时,如果左侧的表达式的计算结果为False ,则整个语句都无法计算为True (False,任何情况均为False),因此不必费力在右侧的表达式中进行计算。 If the left-hand side of expression is True , then the right-hand one is evaluated to see what the full expression should be. 如果表达式的左侧为True ,则对右侧的表达式求值以查看完整的表达式应该是什么。

Similarly, with or clauses, if the expression on the left evaluates to True , there is no way the entire statement can be False since True or anything is always True. 同样,使用or子句,如果左侧的表达式的计算结果为True ,则由于True或其他任何内容始终为True ,因此整个语句都不可能为False So again, the right-hand expression will not be evaluated. 同样,右侧表达式将不被求值。 But if the left-hand expression is False, the right-hand one must be evaluated to know the value of the entire expression. 但是,如果左侧表达式为False,则必须对右侧表达式进行求值以了解整个表达式的值。

You can see this in action here: 您可以在这里看到实际效果:

>>> def foo():
...     print('foo')
...     return 0

>>> def bar():
...     print('bar')
...     return 1

>>> foo() and bar()
foo
0

>>> bar() and foo()
bar
foo
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM