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 -- http://mail.python.org/mailman/listinfo/python-list
