[Python-ideas] Have max and min functions ignore None
Michael Selik
mike at selik.org
Tue Dec 29 21:21:33 EST 2015
If None gets ignored, what about iterables that mix numbers and strings? A
comprehension handles the simple case relatively succinctly and also
handles more complex cases.
max(x for x in iterable if isinstance(x, (int, float)))
If you prefer
is_number = lambda obj: isinstance(obj, numbers.Number)
max(filter(is_number, iterable))
On Tue, Dec 29, 2015 at 6:02 PM Gerald Britton <gerald.britton at gmail.com>
wrote:
> On *Tue Dec 29 06:22:58 EST 2015, Stephen D'Aprano wrote:*
>
>
>> So that's three perfectly reasonable behaviours: max(x, None) is an error
>> and should raise;
>> max(x, None) ignores None and returns x;
>> max(x, None) is unknown or missing and returns None
>> (or some other sentinel representing NA/Missing/Unknown).
>
>
> For comparison's sake, SQL ignores NULL when doing MAX:
>
> e.g.
>
> select max(val)
> from (values (1),(null)) v(val)
>
> returns
>
> 1
>
> In Python, None is sorta-kinda a bit like NULL in SQL, so one could make
> the argument that None should be handled similarly in min and max. OTOH I
> wouldn't want to see Python implement 3-valued logic.
>
>
> --
> Gerald Britton, MCSE-DP, MVP
> LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151230/d2bf4988/attachment.html>
More information about the Python-ideas
mailing list