Proto-PEP: Overloadable Boolean Operators

Andrew Durdin adurdin at gmail.com
Sun Sep 5 07:22:51 EDT 2004


On Sun, 05 Sep 2004 22:48:25 +1200, greg <greg at cosc.canterbury.ac.nz> wrote:
> Discussion is invited on the following proto-PEP.
> 
> -------------------------------------------------------------
> 
> PEP ??? - Overloadable Boolean Operators
> ========================================

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?

class MyBoolean:
    def __init__(self, value):
        self._value = value

    def __not__(self):
        return MyBoolean(not self._value)

    def __and1__(self):
        if(self._value):
            return NeedOtherOperand
        else:
            return self

    def __and2__(self, other):
        return self

    def __or1__(self):
        if(self._value):
            return self
        else:
            return NeedOtherOperand

    def __or2__(self, other):
        return self


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?

Anyway, I'll have to have a close look at your patch -- I'd been
toying with the idea of implementing [or attempting to implement] this
myself.



More information about the Python-list mailing list