about sort and dictionary

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 22 04:05:50 EST 2005


Magnus Lycka wrote:

> Actually, I guess it's possible that sorted() is done so
> that it works like below, but I don't think pre-sorted()
> versions of Python support keyword arguments to list.sort()
> anyway...
> 
> def sorted(l, *p, **kw): s=l[:];s.sort(*p, **kw);return s

One part you missed, sorted is actually closer to:

def sorted(iterable, cmp=None, key=None, reverse=False):
    "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
    s=list(iterable)
    s.sort(cmp, key, reverse)
    return s

The point being that while in general only a list will have a sort 
method, the sorted builtin may be called on any iterable and will 
return a sorted list.

Also note that it only accepts specific named arguments, and has a 
docstring.



More information about the Python-list mailing list