Proto-PEP: Overloadable Boolean Operators
Michael Hudson
mwh at python.net
Tue Sep 7 08:21:50 EDT 2004
aleaxit at yahoo.com (Alex Martelli) writes:
> Andrew Durdin <adurdin at gmail.com> wrote:
>
> > On Tue, 07 Sep 2004 16:37:18 +1200, Greg Ewing
> > <greg at cosc.canterbury.ac.nz> wrote:
> > > Alex Martelli wrote:
> > > > Problem is, __nonzero__ is currently
> > > > typechecked -- it has to return an integer.
> > >
> > > Yes, that's the problem. I should probably elaborate
> > > on that a bit in the PEP.
> >
> > That is not the only issue with __nonzero__ versus __not__ -- in some
> > cases (e.g. the symbolic algebra or SQL query constructor) it is
> > useful to determine when an explicit "not" operator has been used.
>
> Yes, you're right, and my assertion was flawed. Typechecking is only
> part of the issue.
>
>
> > I'm not at a machine with the patch installed on it at the moment, but
> > I just began to whether this patch would have an effect on expressions
> > like (a < b < c) (which are also short-circuiting)... Come to think
> > of it, how do objects which override __gt__ and the other comparisons
> > (particularly for expression construction) work in that case?
>
> I believe a < b < c has exactly the semantics of [but with no doube
> evaluation of b...]:
> (a < b) and (b < c)
>
> Overriding _lt_ to give a print seems to confirm that:
dis.dis can be used to give the same impression:
>>> def f(a, b, c):
... return a < b < c
...
>>> def g(a, b, c):
... return a < b and b < c
...
>>> dis.dis(f)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 DUP_TOP
7 ROT_THREE
8 COMPARE_OP 0 (<)
11 JUMP_IF_FALSE 10 (to 24)
14 POP_TOP
15 LOAD_FAST 2 (c)
18 COMPARE_OP 0 (<)
21 JUMP_FORWARD 2 (to 26)
>> 24 ROT_TWO
25 POP_TOP
>> 26 RETURN_VALUE
27 LOAD_CONST 0 (None)
30 RETURN_VALUE
>>> dis.dis(g)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 COMPARE_OP 0 (<)
9 JUMP_IF_FALSE 10 (to 22)
12 POP_TOP
13 LOAD_FAST 1 (b)
16 LOAD_FAST 2 (c)
19 COMPARE_OP 0 (<)
>> 22 RETURN_VALUE
23 LOAD_CONST 0 (None)
26 RETURN_VALUE
Cheers,
mwh
--
at any rate, I'm satisfied that not only do they know which end of
the pointy thing to hold, but where to poke it for maximum effect.
-- Eric The Read, asr, on google.com
More information about the Python-list
mailing list