Re: Overriding logical operators?

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

  1. On Sat, 21 Aug 2004 16:06:08 -0500, Chris Siebenmann
    <> wrote:
    >
    > Conceptually, 'and' and 'or' (and 'not') don't operate on objects:
    > they operate on truth values (generally derived from objects).


    But in Python, a "truth value" is an object: a bool object.
    Andrew Durdin, Aug 21, 2004
    #1
    1. Advertising

  2. Andrew Durdin <> wrote:
    > On Sat, 21 Aug 2004 16:06:08 -0500, Chris Siebenmann
    > <> wrote:
    > > Conceptually, 'and' and 'or' (and 'not') don't operate on objects:
    > > they operate on truth values (generally derived from objects).

    >
    > But in Python, a "truth value" is an object: a bool object.


    Actually, that's not the whole truth. ;-)

    The logical operators on Python operate on arbitrary objects
    interpreted in a boolean context, but they're not restricted
    to bool objects. Furthermore, the "or" and "and" operators
    return the operand which has been evaluated last, which can
    be an arbitrary object (not neccessarily a bool object). Of
    course, the "not" operator always returns a real bool object
    (either True or False).

    For example, the expression ``"ugh" or 15'' evaluates to the
    string "ugh", and ``0 or []'' evaluates to an empty list.

    So, the logical operators have more built-in magic than just
    short-circuit evaluation. The details are in the Language
    Reference. Providing a clean facility to override them in
    Python would be very difficult, I guess.

    I suggest you simply define your own functions to do what
    you need to do, instead of overriding "or" and "and". If
    you only need to work with boolean values, you could derive
    your own bool class and override the "+" and "*" operators,
    of course.

    Best regards
    Oliver

    --
    Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

    ``All that we see or seem is just a dream within a dream.''
    (E. A. Poe)
    Oliver Fromme, Aug 23, 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. Andrew Durdin

    Overriding logical operators?

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

    Re: Overriding logical operators?

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

    Fwd: Overriding logical operators?

    Andrew Durdin, Aug 21, 2004, in forum: Python
    Replies:
    1
    Views:
    344
    Greg Ewing
    Aug 24, 2004
  4. August Karlstrom

    Precedence of Logical Operators

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

Share This Page