Ternery operator

Richie Hindle richie at entrian.com
Tue Sep 9 08:56:39 EDT 2003


[Me]
> >>> def one():
> 	print 1
> 	return 1
> 
> >>> def two():
> 	print 2
> 	return 2
> 
> >>> (True and [one()] or [two()])[0]
> 1
> 1
> >>> (False and [one()] or [two()])[0]
> 2
> 2
> >>> 
> 
> So it's lazy in the sense of "will not execute the branch not taken",
> which is I'm sure what Uwe meant.  But it's also over-eager in the sense
> of "may execute the taken branch multiple times."

Duh, sorry, I still have my Monday head on.  Ignore that second sentence.
This would have been a better demonstration:

>>> def one():
	print "One"
	return 1

>>> def two():
	print "Two"
	return 2

>>> print (True and [one()] or [two()])[0]
One
1
>>> 

-- 
Richie Hindle
richie at entrian.com






More information about the Python-list mailing list