1

I noticed the 'and' and 'or' logical operators were not defined with a corresponding method in the operator module of the Python standard library. Why can't these operators be overloaded like the others?

Is there official Python documentation that describes this design choice? I could not seem to find any online.

therealblee
  • 421
  • 2
  • 4
  • 7
  • There's a PEP regarding this, https://www.python.org/dev/peps/pep-0335/#rejection-notice – Henrik Andersson Jun 10 '15 at 13:53
  • 1
    Because they *short circuit*. Python will not evaluate the second expression if the first one already determined the outcome. Hooking into the operator would require both operand expressions to be evaluated, breaking the convention (and thus code that relies on it). – Martijn Pieters Jun 10 '15 at 13:55
  • Imagine doing `yourobject and some_function_that_breaks_if_yourobject_is_not_there()`. – Martijn Pieters Jun 10 '15 at 13:57

0 Answers0