Fwd: Overriding logical operators?

Discussion in 'Python' started by Andrew Durdin, Aug 21, 2004.

  1. On Fri, 20 Aug 2004 23:28:20 -0700, Robert Brewer <> 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.
    Andrew Durdin, Aug 21, 2004
    #1
    1. Advertising

  2. Andrew Durdin

    Greg Ewing Guest

    Andrew Durdin wrote:
    > 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)


    That approach would work, but it would slow down the
    evaluation of all logical expressions a lot, requiring
    an extra Python function call to get the second operand,
    even in the common case where no overriding is going on.
    So I doubt it would be accepted.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, Aug 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davide Carboni
    Replies:
    0
    Views:
    348
    Davide Carboni
    Dec 18, 2003
  2. Andrew Durdin

    Overriding logical operators?

    Andrew Durdin, Aug 21, 2004, in forum: Python
    Replies:
    3
    Views:
    408
    Michael Hudson
    Aug 23, 2004
  3. Terry Reedy

    Re: Overriding logical operators?

    Terry Reedy, Aug 21, 2004, in forum: Python
    Replies:
    2
    Views:
    326
    Terry Reedy
    Aug 24, 2004
  4. Andrew Durdin

    Re: Overriding logical operators?

    Andrew Durdin, Aug 21, 2004, in forum: Python
    Replies:
    1
    Views:
    365
    Oliver Fromme
    Aug 23, 2004
  5. August Karlstrom

    Precedence of Logical Operators

    August Karlstrom, Nov 20, 2005, in forum: C Programming
    Replies:
    25
    Views:
    768
    August Karlstrom
    Nov 23, 2005
Loading...

Share This Page