Message94193
Here’s a summary of my research so far (including discussion with other
programmers, a Google code search, discussion on #python-dev on IRC, and
comparing the proposal to other APIs with start-arguments such as sum(),
reduce(), and enumerate()):
1. Showed several examples to other programmers and found that they did
not immediately get what the start argument was trying to do. Was a
start argument the same as:
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
2. There is an issue of API complexity. Even if a feature is useful and
clear, it may not be a good idea when the API of a function is already
complex. In the case of min()/max(), we already have special handling
for one argument (treated as an iterator) versus no arguments (treated
an error versus multiple arguments (treated as an input sequence of
values). We also have a key= argument. Taken together, the min/max
functions already have a lot of features.
3.Beyond the complexity of having too many features in a function that
should be simple, there is also an issue of how those features would
interact:
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?
4. The argument about reduce(max, seq, 0) being confusing to
non-functional programmers isn’t persuasive since perfectly clear
(though multi-line) exception-catching or size-checking imperative forms
can be written, or one can can simply factor-out any recurring
expressions if they seem awkward or confusing:
def max_or_zero(iterable):
'Return zero if the iterable is empty or max(0, max(iterable))
otherwise'
return functools.reduce(max, iterable, 0)
5. In the case of sequences, it can be clearer to write:
max(seq) if seq else 0
max(seq + [0]) # or use itertools.chain()
6. A code search showed that max() is mostly used in a two-argument
form. When it does get used with iterables, it doesn't seem common to
trap ValueErrors. |
|
Date |
User |
Action |
Args |
2009-10-17 23:43:22 | rhettinger | set | recipients:
+ rhettinger, phr, r.david.murray, esam |
2009-10-17 23:43:21 | rhettinger | set | messageid: <1255823001.98.0.554137518559.issue7153@psf.upfronthosting.co.za> |
2009-10-17 23:43:15 | rhettinger | link | issue7153 messages |
2009-10-17 23:43:14 | rhettinger | create | |
|