returning True, False or None

Alex Martelli aleaxit at yahoo.com
Sat Feb 5 05:17:10 EST 2005


Brian van den Broek <bvande at po-box.mcgill.ca> wrote:
   ...
> >>* If all values are None, the function should return None.
> >>* If at least one value is True, the function should return True.
> >>* Otherwise, the function should return False.
   ...
> > for val in (x for x in lst if x is not None):
> >     return val
> > return None
   ...
> These don't do what the OP desired.

Ah, you're right, True should take precedence, point 2 of the specs.


OK, let's take advantage of the fact that None < False < True:

return max(lst)


This fails when lst is empty (the specs presumably imply a None should
be returned then).  More robust:

return max(lst or [None])


Alex



More information about the Python-list mailing list