Fwd: Overriding logical operators?

Andrew Durdin adurdin at gmail.com
Sat Aug 21 18:05:21 EDT 2004


On Fri, 20 Aug 2004 23:28:20 -0700, Robert Brewer <fumanchu at amor.org> wrote:
>
> The first barrier is that 'or' and 'and' get compiled down to jump
> codes, as opposed to operations:

[snip disassembly>]

> ....so you'd be hacking a completely different section of the compiler to
> enable such overriding.

I wasn't yet planning to hack *any* part of the compiler :)

As far as I can see, the compiler changes would be limited to
compiling "a and b" to bytecode equivalent to

    a.__logand__(lambda b: b)

And bool.__logand__ would work like this:

def bool.__logand__(self, otherlambda):
    if not self:
        return False
    else:
        return bool(otherlambda())

I believe this would handle the short-circuiting. How much
implementation this would require is something I don't know the answer
to yet... I've only just started looking at the python source. At the
least it'd mean compiler changes, but probably also changes to a
number of built-in objects.



More information about the Python-list mailing list