returning True, False or None
Mick Krippendorf
mad.mick at gmx.de
Fri Feb 4 18:14:50 EST 2005
Fredrik Lundh wrote:
> Steven Bethard wrote:
>> Raymond Hettinger wrote:
>>>
>>> return max(lst)
>>
>> Very clever! Thanks!
>
> too clever. boolean > None isn't guaranteed by the language
> specification:
>
> http://docs.python.org/ref/comparisons.html
>
> "... objects of different types always compare unequal, and are
> ordered consistently but arbitrarily. /.../ In the future, the
> comparison rules for objects of different types are likely to
> change. ..."
Then maybe like this:
>>> def max_of_two_with_None_less_than_any_other_object(e1, e2):
... if e1 == None:
... return e2
... elif e2 == None:
... return e1
... else:
... return max(e1, e2)
>>> reduce(max_of_two_with_None_less_than_any_other_object, lst)
Mick.
More information about the Python-list
mailing list