Any way to override the operator in Python too?

I tried to override __and__ , but this is for the and operator, and not and - what I want. Can I override and?

+23
python
Jan 23 '09 at 1:36
source share
3 answers

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

+23
Jan 23 '09 at 1:42
source share

No, you cannot override and and or . With the behavior that they have in Python (i.e., short circuiting), they are more like control flow tools than operators, and redefining them will be more like overriding if than + or -.

You can influence the true value of your objects (that is, they are evaluated as true or false) by overriding __nonzero__ (or __bool__ in Python 3).

+32
Jan 23 '09 at 1:44
source share

Not really. There is no special method name for logical short circuit operators.

+1
Jan 23 '09 at 1:41
source share



All Articles