None or 0

Jay O'Connor joconnor at cybermesa.com
Sat Sep 7 12:43:05 EDT 2002


On Sat, 07 Sep 2002 09:08:08 -0700, John Baxter <jwbaxter at spamcop.net>
wrote:

>In article <k7ip4-0p3.ln1 at junker.stroeder.com>,
> Michael Stroder <michael at stroeder.com> wrote:
>
>>  >>> repr(0 or None)
>> 'None'
>>  >>> repr(None or 0)
>> '0'
>>  >>> repr('' or None)
>> 'None'
>>  >>> repr(None or '')
>> "''"
>>  >>>
>> 
>> Is it guaranteed to work like this or should that be avoided?
>
>It seems very convenient to implement or (and and) by returning the 
>first object which decides the answer.

I noticed that happening.  It seems sensible for short-circuiting
that in an "or", you just return th second term , regardless of what
it is


>My guess is that enough people are counting on the behavior that even if 
>the underlying hardware or implementation language were to shift enough 
>to make this implementation quite inconvenient, it would nonetheless 
>survive.
>
>Having said that, I tend not to like counting on it, because it 
>stretches the extra-Python definition of the operators quite a bit.  
>Particularly on the "and" side:
>
>>>> print 5 and 1
>1
>
>Go into (English-speaking) classrooms, and you're going to hear chants 
>of "5 and 1 is 6" not "5 and 1 is 1".

Because in that contexts "and" means "plus" and in this context "and"
means "logical and"

Take care,

Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor


"God himself plays on the bass strings first, when he tunes the soul"



More information about the Python-list mailing list