def BooleanNOT(x):
# not x
if x:
return False
else:
return True
def BooleanOR(x, y):
# x or y
if not x:
return y
else:
return x
def BooleanAND(x, y):
# x and y
if not x:
return x
else:
return y
12345678910111213141516171819202122
逻辑短路
由上一节,可以十分直观地了解到: 在逻辑判断中,并不是每一个判断对象都会被执行.
与运算的第一个对象值为false,则第二个对象会被略过.
或运算的第一个对象值为true,则第二个同样对象会被略过.
这种由于逻辑运算机制而导致的现象就叫做逻辑短路.
>>> def print_but_false():
print("print but false")
return False
>>> True or print_but_false()
True
>>> False and print_but_false()
False
>>> print_but_false() and True
print but false
False
>>>