Any way to override the and operator in Python?

You cannot override the and, or, and not boolean operators.


No you can't override and and or. With the behavior that these have in Python (i.e. short-circuiting) they are more like control flow tools than operators and overriding them would be more like overriding if than + or -.

You can influence the truth value of your objects (i.e. whether they evaluate as true or false) by overriding __nonzero__ (or __bool__ in Python 3).


Not really. There's no special method name for the short-circuit logic operators.

Tags:

Python