Velocity Reviews > Re: Overriding logical operators?

# Re: Overriding logical operators?

Andrew Durdin
Guest
Posts: n/a

 08-21-2004
On Sat, 21 Aug 2004 16:06:08 -0500, Chris Siebenmann
<(E-Mail Removed)> 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.

Oliver Fromme
Guest
Posts: n/a

 08-23-2004
Andrew Durdin <(E-Mail Removed)> wrote:
> On Sat, 21 Aug 2004 16:06:08 -0500, Chris Siebenmann
> <(E-Mail Removed)> 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)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Weng Tianxiang VHDL 12 12-10-2005 03:49 PM Terry Reedy Python 2 08-24-2004 05:49 AM Andrew Durdin Python 1 08-24-2004 05:08 AM Andrew Durdin Python 3 08-23-2004 02:56 PM dangerlee VHDL 4 05-07-2004 07:38 AM