I was reading someone else’s code and he had something like this:
return val1 and val2
I tried this in the Python interpreter and it gave me the latter value on AND while OR gives me the prior value.
So my question is what exactly is happening in that statement?
Thanks.
An expression using
andororshort-circuits when it can determine that the expression will not evaluate to True or False based on the first operand, and returns the last evaluated value:This ‘side-effect’ is often used in python code. Note that
notdoes return a boolean value. See the python documentation on boolean operators for the details.