return A or B ? The other day, I was confused when I saw this code.
python
def hoge:(self)
return A or B
I've never seen or or and in the return syntax, so I'll look it up.
Quote from official documentation
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
I don't know unless I write it.
or_and_in_return.py
def and_in_return1():
return 1 == 1 and 1 == 1
def and_in_return2():
return 1 == 0 and 1 == 1
def and_in_return3():
return 1 == 1 and 1 == 0
print(and_in_return1()) # True
print(and_in_return2()) # False
print(and_in_return3()) # False
def or_in_return1():
return 1 == 1 or 1 == 1
def or_in_return2():
return 1 == 0 or 1 == 0
def or_in_return3():
return 1 == 1 or 1 == 0
print(or_in_return1()) # True
print(or_in_return2()) # False
print(or_in_return3()) # True
It was the same as the action used in if ...
Recommended Posts