Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > keyword argument for min/max

Reply
Thread Tools

keyword argument for min/max

 
 
Steven Bethard
Guest
Posts: n/a
 
      11-30-2004
So I've been playing around with trying to add a keyword argument to min
and max that works similarly to the one for sorted. It wasn't too hard
actually, but it does raise a few questions about proper handling of
keyword arguments. Currently, help(max) gives:

max(...)
max(sequence) -> value
max(a, b, c, ...) -> value

With a single sequence argument, return its largest item.
With two or more arguments, return the largest argument.

Now, if 'key' is to be added as an argument, it cannot be accepted as a
positional argument because the second form of max tells us that, as a
positional argument, the function is just another item to be compared:

>>> d = dict(a=3, b=2, c=1)
>>> max(d, d.__getitem__)

{'a': 3, 'c': 1, 'b': 2}

As you can see, the current implementation compares the dict 'd' and its
__getitem__ method, and determines that 'd' is larger. For backwards
compatibility then, 'key' cannot be accepted as a positional argument.

It is possible to allow 'key' only as a keyword argument, and not as a
positional argument. This is what my current implementation does:

>>> d = dict(a=3, b=2, c=1)
>>> max(d)

'c'
>>> max(d, d.__getitem__)

{'a': 3, 'c': 1, 'b': 2}
>>> max(d, key=d.__getitem__)

'a'

Notice the different behavior when d.__getitem__ is specified as a
keyword argument and when it is specified as a positional argument. My
question is, is this a reasonable approach? It's the only one I could
think of that seems to guarantee backwards compatibility, but it's a
little counterintuitive to me that max(d, d.__getitem__) and
max(d, key=d.__getitem__) don't produce the same thing.

Steve
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      11-30-2004
Steven Bethard wrote:
> So I've been playing around with trying to add a keyword argument to min
> and max that works similarly to the one for sorted. It wasn't too hard
> actually, but it does raise a few questions about proper handling of
> keyword arguments.


Sorry to reply to my own post, but I thought of another question about
adding this feature. Should the following work?

..>>> max((2, 'a'), (1, 'b'), key=operator.itemgetter(1))

That is, should you be able to specify a 'key' argument as a keyword
parameter after a list of positional arguments (to be min/maxed).

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyword argument 'from'; invalid syntax Kai Kuehne Python 4 07-26-2007 05:50 AM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
getting a class attribute using a keyword argument Guy Robinson Python 3 01-19-2005 11:01 PM
a question on the new dict() keyword argument syntax Oktay Safak Python 2 08-02-2003 09:12 AM



Advertisments