None or 0
John Baxter
jwbaxter at spamcop.net
Sat Sep 7 12:08:08 EDT 2002
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.
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".
--John
More information about the Python-list
mailing list