[Python-Dev] PEP 285 (Adding a bool type) extending it further
Marcelo Matus
mmatus@dinha.acms.arizona.edu
Thu, 03 Oct 2002 16:45:56 -0700
Given my silly problems trying to write a class which overwrite logical
operations
like 'and', 'or' and 'not', which is not possible by now
(see the discussion following
http://mail.python.org/pipermail/python-dev/2002-October/029122.html )
I would like to propose to extend the PEP 285
to add boolean operations that can be overloaded as the rest of the
operations in python.
The operations in question refers of course to 'and', 'or' and 'not'.
I guess this will involve to add a structure similar to PyNumberMethods
or PySequenceMethods, but for boolean methods, something like:
typedef struct {
binaryfunc bl_and;
binaryfunc bl_or;
binaryfunc bl_not;
} PyBooleanMethods;
where the bl_and, bl_or and bl_not methods refer to the
boolean operations 'and', 'or' and 'not' of course.
This methods will be the one used to implement the
corresponding operations, and they could be overloaded
by special methods which names could be
__land__, __lor__ and __lnot__
to distinguish them from the numeric bitwise versions
__and__ and __or__.
In the case that there is no logical special methods
defined in the class, the bolean methods must
behave as they do right now. However, if the
user overload them, they could return anything,
just like the "rich comparison methods" >=, <=, < and >.
Therefore you can write code like this
class MyClass:
def __land__(self, other):
.....
def __lor__(self, other):
.....
def _lnot__(self, other):
......
a = MyClass(1)
b = MyClass(2)
c = MyClass(3)
res = a >= b and not (b < c)
and 'res' could be anything that in the "MyClass" makes sense.
Marcelo