[New-bugs-announce] [issue34149] Behavior of the min/max with key=None

Alexander Marshalov report at bugs.python.org
Wed Jul 18 12:06:09 EDT 2018


New submission from Alexander Marshalov <_ at marshalov.org>:

I was faced with the fact that the behavior of the functions "min"/"max" and "sorted" is a little different.

For example, this code works fine:

    >>> sorted([3, 2, 1], key=None)
    [1, 2, 3]

But the same example for "min" and "max" doesn't work:

    >>> min([3, 2, 1], key=None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'NoneType' object is not callable

That is why the heapq library has this code:

    ...
    def nsmallest(n, iterable, key=None):
        ...
            if key is None:
                result = min(it, default=sentinel)
            else:
                result = min(it, default=sentinel, key=key)
        ...

At the same time, there are many places where such checks are not performed for the "sorted" function. I think the behavior of the "min" / "max" / "sorted" functions should be unified. That is, they should work as if "None" is the default value for "key".

----------
messages: 321891
nosy: amper
priority: normal
severity: normal
status: open
title: Behavior of the min/max with key=None
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34149>
_______________________________________


More information about the New-bugs-announce mailing list