Regarding the magical "or" operator
David Ascher
da at ski.org
Sat Aug 14 17:19:48 EDT 1999
Hi Tom!
On 14 Aug 1999, Tom Christiansen wrote:
> I was wondering how often most python programmers make use of that
> remarkably nifty property of the logical operators, in which they
> produce not merely 0 or 1, but the value is last looked at when it
> decided whether the expression was true or false.
I'm not sure about most python programmers. It's a feature of the
language that I always teach in my courses, and I tend to get blank looks
at first. I find that once I motivate the or behavior with an example
like:
title = windowname or 'Untitled'
a few folks think it's neat (and I guess from that that they plan on using
it, or at least trying it). I use it periodically, although not very
often.
Not that you asked, but I find the 'and' behavior much harder to motivate
and grok, and I avoid it personally.
> My question is this: have you ever had an urge for it (or a variant)
> to care about the difference between None and any other false value?
[...]
> The idea would be a version of "or" that would return the first non-None
> value. For example, supposing this were called "nor" (silly name,
> but this is *not* a proposal):
>
> print f() nor h()
> []
>
> print g() nor h()
> ['Some']
>
> The argument is that this would be used not with prints, but with
> assignments, as in
>
> a = f() nor g()
>
> The reason I ask is not because I actually want such a thing. Gosh, no.
> I'm rather uncomfortable with the idea. I'm just trying to gather some
> understanding and perspective for use with the folks who *would* like
> such an oddity. :-)
FWIW, it doesn't grab me. I'd read 'a nor b' as 'not (a or b)', and I
know too much about truth and falsehood in Python to expect None to be
treated differently.
(It's a nice word, though. One that, alas, Dr. Seuss didn't use when he
should have in One Fish, Two Fish, Red Fish, Blue Fish.)
--david ascher
More information about the Python-list
mailing list