Python中的 “and” 评估顺序

huangapple go评论56阅读模式
英文:

python "and" evaluation sequence

问题

问题:为什么像(True and a)这样的表达式不会在括号内完全求值?

在Python中,逻辑运算符(如and)是短路运算符,它们只在需要时才会计算表达式的值。对于and运算符,如果第一个表达式是False,则整个表达式将返回False,并且不会计算第二个表达式的值。因此,如果第一个表达式是True,则会继续计算第二个表达式。

在你的示例中,(True and a),由于第一个表达式是True,Python会继续计算第二个表达式a的值,而不会短路。这与你的预期不同,因为你可能期望由于第一个表达式已经为True,所以整个表达式会直接返回True,而不会计算第二个表达式。

这是Python的设计决定,以便在逻辑表达式中能够正确处理各种情况。如果你想要在第一个表达式为True时直接返回True,可以使用条件表达式或者其他方法来实现你的预期行为。

英文:

I am trying to understand how python keywords and operators interact with object dunder methods and have encounter a situation I don't quite understand.

Setup:
I created two simple classes with the __bool__ dunder method that I believe should be used when checking for truthy conditions. I have them always return true which should be the same behavior as if the method was not defined on the class, but I have added a print statement so I can see when the method is called.

class ClassA(object):

	def __bool__(self):
		print('__bool__ check classA')
		return True

class ClassB(object):

	def __bool__(self):
		print('__bool__ check classB')
		return True

Test 1:

if a and b: print(True)

The output is what I expected to see:

__bool__ check classA
__bool__ check classB
True

Test 2:

c = (a and b)
print(c)

The output is NOT what I expected to see:

__bool__ check classA
<__main__.ClassB object at 0x0000020ECC6E7F10>

My best guess is that Python is evaluating the logic from left to right, but doesn't call the __bool__ method on the final object until needed. (I don't understand why, but I think that is what is happening)

Test 3:

c = (a and b and a)
print(c)

This output agrees with my assumption.

__bool__ check classA
__bool__ check classB
<__main__.ClassA object at 0x0000026A81FA7FD0>

Test 4:

c = (a and b and a)
if c: print(True)

Further calling if c then evaluates the check on the object.

__bool__ check classA
__bool__ check classB
__bool__ check classA
True

Question
Why doesn't an expression such as (True and a) fully evaluate inside the parenthesis?

答案1

得分: 3

表达式 a and b 如果 a 为假,则返回左操作数,如果 a 为真,则返回右操作数。不需要评估 b 的布尔值。

然而,在 if 语句中使用时,有必要检查整个表达式是否产生一个真值。

英文:

The expression a and b returns the left hand operand if a is falsy and returns the right hand operand if a is truthy. There is no need to evaluate the boolean value of b.

However, when used in an if statement, it is necessary to check if the entire expression gives a truthy value.

huangapple
  • 本文由 发表于 2023年2月18日 00:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定