and and or are operators, like + and -, and cannot be assigned to variables. Unlike + et al., there are no functions that implement them, due to short-circuting: a and b only evaluates b if a has a truthy value, while foo(a, b) must evaluate both a and b before foo is called.
The closest equivalent would be the any and all functions, each of which returns true as soon as it finds a true or false value, respectively, in its argument.
>>> any([1+2==3, 3+1==5]) # Only needs the first element of the list
True
>>> all([3+1==5, 1+2==3]) # Only needs the first element of the list
False
Since these are ordinary functions, you can bind them to a variable.
if True:
logical_obj = any
else:
logical_obj = all
if logical_obj([1 + 2 == 3, 3 + 1 == 5]):
pass
The list has to be fully evaluated before the function can be called, but if the iterable is lazily generated, you can prevent expressions from being evaluated until necessary:
>>> def values():
... yield 1 + 2 == 3
... print("Not reached by any()")
... yield 3 + 1 == 5
... print("Not reached by all()")
...
>>> any(values())
True
>>> all(values())
Not reached by any()
False
operatordoes not expose functions forandoror. In a pinch bitwise operators could be used, but that would probably justify an answer.1 or 3 == 4probably isn't what you want anyway, but1 == 4 or 3 == 4.any([1 == 4, 3==4])orall([1==4, 3==4]), as desired.anyandallto a variable.)