mirror of
https://github.com/python/cpython.git
synced 2025-10-08 08:01:55 +00:00
bpo-34149: Behavior of the min/max with key=None (GH-8328)
Improve consistency with the signature for sorted(), heapq.nsmallest(), heapq.nlargest(), and itertools.groupby().
This commit is contained in:
parent
bde782bb59
commit
e22072fb11
5 changed files with 17 additions and 8 deletions
10
Lib/heapq.py
10
Lib/heapq.py
|
@ -468,10 +468,7 @@ def nsmallest(n, iterable, key=None):
|
|||
if n == 1:
|
||||
it = iter(iterable)
|
||||
sentinel = object()
|
||||
if key is None:
|
||||
result = min(it, default=sentinel)
|
||||
else:
|
||||
result = min(it, default=sentinel, key=key)
|
||||
result = min(it, default=sentinel, key=key)
|
||||
return [] if result is sentinel else [result]
|
||||
|
||||
# When n>=size, it's faster to use sorted()
|
||||
|
@ -531,10 +528,7 @@ def nlargest(n, iterable, key=None):
|
|||
if n == 1:
|
||||
it = iter(iterable)
|
||||
sentinel = object()
|
||||
if key is None:
|
||||
result = max(it, default=sentinel)
|
||||
else:
|
||||
result = max(it, default=sentinel, key=key)
|
||||
result = max(it, default=sentinel, key=key)
|
||||
return [] if result is sentinel else [result]
|
||||
|
||||
# When n>=size, it's faster to use sorted()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue