returning True, False or None

Jeremy Bowers jerf at jerf.org
Fri Feb 4 12:42:01 EST 2005


On Fri, 04 Feb 2005 16:44:48 -0500, Daniel Bickett wrote:
> [ False , False , True , None ]
> 
> False would be returned upon inspection of the first index, even
> though True was in fact in the list. The same is true of the code of
> Jeremy Bowers, Steve Juranich, and Jeff Shannon. As for Raymond
> Hettinger, I can't even be sure ;)

Nope. To recall, my code was: 

seenFalse = False
for item in list:
	if item: return True
	if item is False: seenFalse = True
if seenFalse:
	return False
return None

So, turning that into a real function and not a sketch:

Python 2.3.4 (#1, Jan 25 2005, 21:29:33) 
[GCC 3.4.3  (Gentoo Linux 3.4.3, ssp-3.4.3-0, pie-8.7.6.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def thingy(l):
...     seenFalse = False
...     for item in l:
...             if item: return True
...             if item is False: seenFalse = True
...     if seenFalse:
...             return False
...     return None
... 
>>> thingy([ False , False , True , None ])
True
>>> 

The defense rests, your honor. :-)

(I like the later use of "returnValue" and the reduce solution was cute
and quite educational (very appropriate here). I deliberately eschewed
fanciness, though.)



More information about the Python-list mailing list