[Python-Dev] Re: PEP 285 (Adding a bool type) extending it further
Marcelo Matus
mmatus@dinha.acms.arizona.edu
Thu, 03 Oct 2002 17:25:01 -0700
To solve the problem of short-circuiting 'and' and 'or',
the BooleanMethod class could be tested for non
zero method components, ie, you befine
the methods to null, like
PyBooleanMethods {
binaryfunc bl_and;
binaryfunc bl_or;
binaryfunc bl_not;
} PyBooleanMethods;
static PyBooleanMethods default_as_boolean = {
0, /* bl_and */
0, /* bl_or */
0, /* bl_not */
};
and overwrite them only when the __land__, __lor__
or __lnot__ are defined.
Then, when you perfom the 'add' or 'or' operations, you
check for the proper boolean method in the first
argument only, which anyway have to be evaluated.
If the boolean method exists, then you
force the evaluation of the second argument
and the apply the proper method. If not, you
do the usual, which will be the traditional short-circuiting
operation, ie:
a and b
will be equivalent to
if a.bl_and exist:
return a.bl_and(b)
else
return a && b (traditional form with
short-circuiting)
and similar for 'or'.
In this way, the implementation will be fully compatible with all
the previous code, allowing short-circuiting for the classes
that doesn't define the boolean methods (all of them by now),
but will allow you to extend the operation for classes with
their own version of the logical operations.
Of course this requires that if you want to use your own
logical operations, both or at least the first
argument in the operation must always be one of your special
classes. But with proper documentations and warnings about it,
I guess this alternative is better than nothing.
Marcelo