Regarding the magical "or" operator

Tom Christiansen tchrist at mox.perl.com
Sat Aug 14 17:52:34 EDT 1999


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.

For example:

	print [] or "Some"
    Some

	print "Peter" and "Paul"
    Paul

	print (20 - 4*5) or None
    None

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?
I am certainly *not* asking for such a thing.  I'd just like to know
whether anyone ever cared.  For example, given these three functions:

	def f():
	    return []

	def g():
	    return None

	def h():
	    return ["Some"]

Consider these prints:

	print f() or h()
    ['Some']

	print g() or h()
    ['Some']

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. :-)

--tom
-- 
Mister Catbert, the company is trying to force me to use a different kind
of computer.  You're the human resources directory. what are you doing
to sop this religious persecution?!  What every happened to "Diversity"??
The longer you verk here, diverse it gets. next.  --Scott Adams, "Dilbert"




More information about the Python-list mailing list