[Python-Dev] PEP 207 -- Rich Comparisons

Greg Ewing greg@cosc.canterbury.ac.nz
Tue, 19 Dec 2000 16:07:56 +1300 (NZDT)


> The problem I have with this is that the code to evaluate g() has to
> be generated twice!

I have an idea how to fix that. There need to be two methods,
__boolean_and_1__ and __boolean_and_2__. The first operand
is evaluated and passed to __boolean_and_1__. If it returns
a result, that becomes the result of the expression, and the
second operand is short-circuited.

If __boolean_and_1__ raises a NeedOtherOperand exception
(or there is no __boolean_and_1__ method), the second operand 
is evaluated, and both operands are passed to __boolean_and_2__.

The bytecode would look something like

        <evaluate first operand>
        BOOLEAN_AND_1 label
        <evaluate second operand>
        BOOLEAN_AND_2
label:  ...

I'll make a PEP out of this one day if I get enthusiastic
enough.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+