Overriding logical operators?

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

  1. In Python, you can override the behaviour of most operators for a
    class, by defining __add__, __gt__, and the other special object
    methods.

    I noticed that, although there are special methods for most operators,
    they are conspicuously absent for the logical "or" and "and". I'm
    guessing that the reason for this is that these operators
    short-circuit if their first operand answers the whole question?

    Would it be possible to allow overriding the logical operators, with
    the caveat that overriding it would prevent short-circuiting?
    Andrew Durdin, Aug 21, 2004
    #1
    1. Advertising

  2. In article <>,
    Andrew Durdin <> wrote:

    > In Python, you can override the behaviour of most operators for a
    > class, by defining __add__, __gt__, and the other special object
    > methods.
    >
    > I noticed that, although there are special methods for most operators,
    > they are conspicuously absent for the logical "or" and "and". I'm
    > guessing that the reason for this is that these operators
    > short-circuit if their first operand answers the whole question?
    >
    > Would it be possible to allow overriding the logical operators, with
    > the caveat that overriding it would prevent short-circuiting?


    The implementation of "and" and "or" are special, as others have pointed
    out. However, you do have some control over how they behave for objects
    of user-defined classes. By overriding __nonzero_, you can define
    whatever truth value you like for instances of a user-defined class.
    See:

    http://docs.python.org/lib/truth.html

    This would not, in itself, "override" the behaviour of "and" and "or",
    in the sense that you were describing, but it would let you control the
    outcome for objects of your particular class.

    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
    Michael J. Fromberger, Aug 22, 2004
    #2
    1. Advertising

  3. On Sat, 21 Aug 2004 21:38:01 -0400, Michael J. Fromberger
    <> wrote:
    >
    > The implementation of "and" and "or" are special, as others have pointed
    > out. However, you do have some control over how they behave for objects
    > of user-defined classes. By overriding __nonzero_, you can define
    > whatever truth value you like for instances of a user-defined class.


    I suppose now I ought to say why I need to to do more than that. I've
    got a situation where I want to construct an expression, but evaluate
    it later when I get more information. I came up with a "DelayedEval"
    class, the instances of which can have operations performed on them,
    but the result is not evaluated until a particular method is called.
    I'm currently overriding all the operators I can, so that these
    instances can be handled in a fairly normal fashion for the most part.
    For example:

    foo = DelayedEval()
    foo = (-foo + 15) * 3
    foo_positive = (foo >= 0)

    print foo.evaluate(10) # prints 15
    print foo.evaluate(20) # prints -15
    print foo_positive.evaluate(10) # prints True
    print foo_positive.evaluate(20) # prints False

    In this situation, merely overriding the __nonzero__ method will not
    allow me to delay the evaluation of the logical boolean operators; to
    do that I need to be able to override them.

    The alternative solution that I can see to the issue is to use lambdas
    to create the expressions I need, and calling them to evaluate them,
    e.g.:

    foo = lambda v: (-v + 15) * 3
    foo_positive = lambda v: (foo(v) >= 0)

    This will work, but having to manually chain the evaluation in this
    way is a little awkward. It also doesn't allow me to print the
    expressions. With the DelayedEval class, I can do this:

    print foo_positive # prints (((-(value) + 15) * 3) >= 0)
    Andrew Durdin, Aug 22, 2004
    #3
  4. Andrew Durdin <> writes:

    > In Python, you can override the behaviour of most operators for a
    > class, by defining __add__, __gt__, and the other special object
    > methods.
    >
    > I noticed that, although there are special methods for most operators,
    > they are conspicuously absent for the logical "or" and "and".


    That's because they're not operators, they're flow control constructs.
    Well, maybe that's not a helpful answer, but it's one you haven't had
    yet :)

    > Would it be possible to allow overriding the logical operators, with
    > the caveat that overriding it would prevent short-circuiting?


    I think this would be a foul abomination. But maybe that's just me.
    (I don't really approve of operator overloading either).

    Cheers,
    mwh

    --
    Ability to type on a computer terminal is no guarantee of sanity,
    intelligence, or common sense.
    -- Gene Spafford's Axiom #2 of Usenet
    Michael Hudson, Aug 23, 2004
    #4
    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. Terry Reedy

    Re: Overriding logical operators?

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

    Fwd: Overriding logical operators?

    Andrew Durdin, Aug 21, 2004, in forum: Python
    Replies:
    1
    Views:
    330
    Greg Ewing
    Aug 24, 2004
  3. Andrew Durdin

    Re: Overriding logical operators?

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

    Precedence of Logical Operators

    August Karlstrom, Nov 20, 2005, in forum: C Programming
    Replies:
    25
    Views:
    775
    August Karlstrom
    Nov 23, 2005
  5. Replies:
    7
    Views:
    476
    Mike Treseler
    Jul 12, 2007
Loading...

Share This Page