[issue22979] Use of None in min and max

2014-12-12 Thread Steven D'Aprano

Steven D'Aprano added the comment:

Even though I agree with closing this issue, there is some support for ignoring 
certain "missing values" when calculating min() and max(). The draft 2008 
revision of IEEE-754 includes two functions maxNum and minNum which silently 
skip over NANs:

https://en.wikipedia.org/wiki/IEEE_754_revision#min_and_max

The R language also supports max and min skipping over missing values:

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html

The problem is that there are so many different things which somebody might 
want to do, it is hard to tell which (if any) the built-ins should support:

- silently skip over None
- or NANs
- or treat some other, user-specified, value as "missing"
- treat None as the smallest (or largest) value
- treat the presence of None as an error
- etc.

I think that min() and max() should continue to be relatively simple-minded and 
let users write their own more complex versions if needed, e.g. by calling 
min(filter(condition, values)).

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22979] Use of None in min and max

2014-12-02 Thread R. David Murray

R. David Murray added the comment:

Just select your initial value as something that works with the sequence you 
are iterating.  If necessary, you can define custom 'always maximum' and 
'always minimum' objects.  (You could try proposing builtin objects with that 
feature on the python-ideas mailing list, if you want to pursue this, but since 
this is a specialized need and usually you don't even need custom objects, I 
suspect you won't get much enthusiasm.  But I could be wrong, especially if you 
can some up with additional use cases.

--
nosy: +r.david.murray
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22979] Use of None in min and max

2014-12-02 Thread Simeon Visser

Simeon Visser added the comment:

So, to clarify, as the problem no longer occurs in Python 3 (as it requires the 
caller to provide only orderable objects) I'm not sure a meaningful change can 
be made here. It would require changing the behaviour of min/max in Python 
2.7.x in a way that could break existing code.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22979] Use of None in min and max

2014-12-02 Thread Simeon Visser

Simeon Visser added the comment:

This doesn't happen in Python 3 as None can't be compared to other elements:

>>> min([1,2,3,None])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: NoneType() < int()

I can also imagine people now using min with the intended behaviour of "give me 
the smallest element, or None if it happens to be present".

--
nosy: +simeon.visser

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22979] Use of None in min and max

2014-12-02 Thread Maytag Metalark

New submission from Maytag Metalark:

`None` should never be the result of the built-in `min` and `max` functions. 
When `None` is supplied as one of the values to check, it should never be 
chosen as the result.

This would make it much easier to find a minimum and/or maximum while iterating 
over values. For instance, the following is a common pattern:

mn = None
mx = None
for x in iterable:
if mn is None or x < mn:
mn = x
if mx is None or x > mx:
mx = x

Note that although the `min` and `max` functions could be applied directly to 
`iterable` in the above case, the above pattern is more efficient (only once 
through the loop) and covers the common case where additional operations are 
performed on each value of the iterable.

If the suggested enhancement was made, the above code could be written more 
simply as:

mn = None
mx = None
for x in iterable:
mn = min(mn, x)
mx = max(mx, x)

At present, this will actually work for `max`, as None evaluates as less than 
every number, but it will not work for `min` (for the same reason).

The suggested change would mean that None is simultaneously greater than and 
less than every other value, but that only matters if we assume a total 
ordering of all the values including None, which doesn't seem like it would be 
important.

--
messages: 231998
nosy: Brian.Mearns
priority: normal
severity: normal
status: open
title: Use of None in min and max
type: enhancement
versions: Python 2.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com