New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add "start" arg to max and min functions #51402
Comments
Lots of times I want to find the largest element of a list or sequence, max with multiple args is already overloaded to mean the maximum of the The alternatives to using reduce aren't so attractive. If seq happens |
In case you don't remember it, this thread from python-ideas is relevant: http://mail.python.org/pipermail/python-ideas/2009-April/004107.html I can't tell from rereading the thread whether the support was for the |
David, I'm not on that mailing list so hadn't seen the earlier My proposed python implementation: def mymax(*args, **kwargs):
if len(args) > 1: return max(*args)
if len(args) == 0: raise TypeError, "mymax needs at least one
positional arg"
if 'initial' in kwargs: return reduce(max,args[0],kwargs['initial'])
return reduce(max,args[0]) |
Sorry, that "in case you don't remember' was directed at Raymond, not you. Since Raymond took ownership of this issue I don't think he's dismissing Note that 'reduce' is _not_ a builtin in Python3, although it is still |
Here’s a summary of my research so far (including discussion with other
reduce(max, seq, 0) # zero when empty and never less than zero
max(seq) if seq else 0 # zero when empty (only works for sequences)
max(chain([0], seq) # zero when empty and never less than zero
3.Beyond the complexity of having too many features in a function that min(iterable, key=f, start=x) # is the default value x or f(x)?
min(start=x) # should this be allowed?
min(*args, start=x) # if so, what is behavior when len(args)==0 or 1
or 2?
def max_or_zero(iterable):
'Return zero if the iterable is empty or max(0, max(iterable))
otherwise'
return functools.reduce(max, iterable, 0)
max(seq) if seq else 0
|
|
Thanks for the thoughtful reply. To give the idea another chance, "What does this function return? The person had a hard time making any sense of it. Perhaps the start I'm rejecting the feature request for several reasons:
Thank you for the feature request. Sorry, this one didn't pan out. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: