Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Overriding logical operators?

Reply
Thread Tools

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.
 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it legal to write an logical equation for a FPGA LUT in claims of a patent? Weng Tianxiang VHDL 12 12-10-2005 03:49 PM
Re: Overriding logical operators? Terry Reedy Python 2 08-24-2004 05:49 AM
Fwd: Overriding logical operators? Andrew Durdin Python 1 08-24-2004 05:08 AM
Overriding logical operators? Andrew Durdin Python 3 08-23-2004 02:56 PM
logical left shifter or latch ?? dangerlee VHDL 4 05-07-2004 07:38 AM



Advertisments