[Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence (original) (raw)
Ron Adam rrr at ronadam.com
Fri Oct 15 19:13:54 CEST 2010
- Previous message: [Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence
- Next message: [Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
My apologies, I clicked "reply" instead of "reply list" last night.
After thinking about this a bit more, it isn't a matter of never needing to do it. The min and max functions wouldn't be able to compare a series of lists as individual values without a keyword switch to choose the specific behavior for a single item. ie... list of items or an item that happens to be a list. The below examples would not be able to compare sequences correctly.
Ron
On 10/14/2010 09:14 PM, Guido van Rossum wrote:
Why would you ever want to write min(1)? (Or min(x) where x is not iterable.)
Basically to allow easier duck typing without having to check weather x is an iterable.
This isn't a big deal or a must have. It's just one solution to a problem presented here. My own thoughts is that little tweaks like this may be helpful when using functions in indirect ways where it's nice not to have to do additional value, type, or attribute checking.
[Tal also says]
As Guido mentioned, there is never a reason to do max(value) where value is not an iterable.
Well, you can always avoid doing it, but that doesn't mean it wouldn't be nice to have sometimes. Take a look at the following three coroutines that do the same exact thing. Which is easier to read and which would be considered the more Pythonic.
def xmin(*args, **kwds): # Allow min to work with a single non-iterable value. if len(args) == 1 and not hasattr(args[0], "iter"): return min(args, **kwds) else: return min(*args, **kwds)
Accept values or chunks of values and keep a running minimum.
@consumer def Running_Min(out_value=None): while 1: in_value = yield out_value if in_value is not None: if out_value is None: out_value = xmin(in_value) else: out_value = xmin(out_value, xmin(in_value))
@consumer def Running_Min(out_value=None): while 1: in_value = yield out_value if in_value is not None: if not hasattr(in_value, "iter"): in_value = [in_value] if out_value is None: out_value = min(in_value) else: out_value = min(out_value, min(in_value))
@consumer def Running_Min(out_value=None): while 1: in_value = yield out_value if in_value is not None: if not hasattr(in_value, "iter"): if out_value is None: out_value = in_value else: out_value = min(out_value, in_value) else: if out_value is None: out_value = min(in_value) else: out_value = min(out_value, min(in_value))
- Previous message: [Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence
- Next message: [Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]