max max - 4 months ago 10
Python Question

Why doesn't the operator module have a function for logical or?

In Python 3, operator.or_ is equivalent to the bitwise

|
, not the logical
or
. Why is there no operator for the logical
or
?

Answer

The or and and operators can't be expressed as functions because of their short-circuiting behavior:

False and some_function()
True or some_function()

in these cases, some_function() is never called.

A hypothetical or_(True, some_function()), on the other hand, would have to call some_function(), because function arguments are always evaluated before the function is called.