Proto-PEP: Overloadable Boolean Operators

greg greg at cosc.canterbury.ac.nz
Sun Sep 5 08:09:08 EDT 2004


Andrew Durdin wrote:
> If I understand this correctly, then logical operations on instances
> of the following class should duplicate the existing behaviour for the
> boolean type -- is this correct?
> 
>     def __and2__(self, other):
>         return self

That should be

           return other

and similarly for __or2__. Otherwise it looks right.

> The PEP doesn't explicitly describe when __rand2__ (or __ror2__) is
> used. I'd guess that it will be called for (A and B) when A defines
> neither __and1__ nor __and2__ -- is this correct?

Not exactly. It will get called whenever the second operand
is needed, the first operand doesn't define __and2__, and the
second operand does define __rand2__.

The exact rules are rather complicated to state completely. I
have some text which spells it all out in mind-numbing detail,
but I left it out of the first draft of the PEP for fear of
scaring people off before I'd got the idea across. Perhaps
I'll include it as an appendix or something.

Greg





More information about the Python-list mailing list