📜  Python中的短路技术

📅  最后修改于: 2022-05-13 01:55:38.754000             🧑  作者: Mango

Python中的短路技术

短路是指如果表达式的真值已经确定,则停止执行布尔运算。表达式的评估从左到右进行。在Python中,各种布尔运算符和函数支持短路。

布尔运算符中的短路

下面给出的图表可以深入了解布尔表达式的短路情况。布尔运算符按优先级升序排列。

布尔

or:当Python解释器扫描表达时,它接受第一条语句并检查它是否为真。如果第一个语句为真,则Python返回该对象的值而不检查第二个语句。该程序不会打扰第二条语句。如果第一个值为 false,则Python仅检查第二个值,然后结果基于后半部分。
and:对于and表达式, Python使用短路技术来检查第一个语句是否为假,那么整个语句必须为假,因此它返回该值。只有当第一个值为真时,它才会检查第二个语句并返回该值。
当表达式的真值已经达到时,包含andor的表达式停止执行。评估从左到右进行。

Python3
# python code to demonstrate short circuiting 
# using and and or
   
# helper function
def check():
    return "geeks"
   
# using an expression to demonstrate
# prints "geeks", and gets executed 
# as both are required to check truth value
print (1 and check())
   
       
# using an expression to demonstrate
# prints 1
# as only if 1st value is true, or 
# doesnt require call check() fun
print (1 or check())
   
# using an expression to demonstrate
# prints "geeks"
# the value returns true when check 
# is encountered. 1 is not executed
print (0 or check() or 1)
   
# using an expression to demonstrate
# prints 1
# as last value is required to evaluate
# full expression because of "and"
print (0 or check() and 1)


Python3
# python code to demonstrate short circuiting 
# using all() and any()
   
# helper function
def check(i):
    print ("geeks")
    return i
   
# using all()
# stops execution when false occurs
# tells the compiler that even if one is 
# false, all cannot be true, hence stop 
# execution further.
# prints 3 "geeks" 
print (all(check(i) for i in [1, 1, 0, 0, 3]))
   
print("\r")
   
# using any()
# stops execution when true occurs
# tells the compiler that even if one is 
# true, expression is true, hence stop 
# execution further.
# prints 4 "geeks" 
print (any(check(i) for i in [0, 0, 0, 1, 3]))


Python3
# python code to demonstrate short circuiting 
# using conditional operators
   
# helper function
def check(i):
    print ("geeks")
    return i
   
# using conditional expressions
# since 10 is not greater than 11
# further execution is not taken place 
# to check for truth value.
print( 10 > 11 > check(3) )
   
print ("\r")
   
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place 
# to check for truth value.
# return true as 11 > 3
print( 10 < 11 > check(3) )
   
   
print ("\r")
   
   
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place 
# to check for truth value.
# return false as 11 < 12
print( 10 < 11 > check(12) )


输出:

geeks
1
geeks
1
all() 和 any() 短路

Python中的内置函数 all() 和 any() 也支持短路。下面的示例将使您清楚地了解它是如何工作的。

Python3

# python code to demonstrate short circuiting 
# using all() and any()
   
# helper function
def check(i):
    print ("geeks")
    return i
   
# using all()
# stops execution when false occurs
# tells the compiler that even if one is 
# false, all cannot be true, hence stop 
# execution further.
# prints 3 "geeks" 
print (all(check(i) for i in [1, 1, 0, 0, 3]))
   
print("\r")
   
# using any()
# stops execution when true occurs
# tells the compiler that even if one is 
# true, expression is true, hence stop 
# execution further.
# prints 4 "geeks" 
print (any(check(i) for i in [0, 0, 0, 1, 3]))

输出:

geeks
geeks
geeks
False

geeks
geeks
geeks
geeks
True
条件运算符中的短路

条件运算符也遵循短路,因为当获得表达式结果时,不需要进一步执行。

Python3

# python code to demonstrate short circuiting 
# using conditional operators
   
# helper function
def check(i):
    print ("geeks")
    return i
   
# using conditional expressions
# since 10 is not greater than 11
# further execution is not taken place 
# to check for truth value.
print( 10 > 11 > check(3) )
   
print ("\r")
   
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place 
# to check for truth value.
# return true as 11 > 3
print( 10 < 11 > check(3) )
   
   
print ("\r")
   
   
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place 
# to check for truth value.
# return false as 11 < 12
print( 10 < 11 > check(12) )

输出:

False

geeks
True

geeks
False