is this sort method the same as the one in python 2.4

Pedro Werneck pedro.werneck at terra.com.br
Sun Jan 30 17:49:01 EST 2005


What about this ?


#
if sys.version_info >= (2,4):
    def sorted(iterable, *args, **kwds):
        seq = list(iterable)
        seq.sort(*args, **kwds)
        return seq
#

It worked against the TestSorted in lib/test/test_builtins.py





On Sun, 30 Jan 2005 08:30:17 +0100
"Fredrik Lundh" <fredrik at pythonware.com> wrote:

> Raymond Hettinger wrote:
> 
> >> I'm trying to emulate the sorted() method introduced in python 2.4. The
> >> only difference is that it takes a sequence as one of its arguments
> >> rather than being a method of the sequence class. Does my method do the
> >> same as the sorted()?
> >
> > Almost.  This is closer to the mark:
> >
> > def sorted(iterable, cmp=None, key=None, reverse=False):
> >    "return a sorted copy of its input"
> >    if sys.version_info >= (2,4):
> >        return sorted(iterable, cmp, key, reverse)
> 
> with your code
> 
>     print sorted([1, 2, 3])
> 
> gives me a traceback that ends with
> 
>   File "test.py", line 6, in sorted
>     return sorted(iterable, cmp, key, reverse)
>   File "test.py", line 6, in sorted
>     return sorted(iterable, cmp, key, reverse)
>   File "test.py", line 6, in sorted
>     return sorted(iterable, cmp, key, reverse)
>   File "test.py", line 5, in sorted
>     if sys.version_info >= (2,4):
> RuntimeError: maximum recursion depth exceeded in cmp
> 
> the recursion isn't really that hard to explain, but the runtime error doesn't
> really seem right...
> 
> :::
> 
> to fix the recursion, move the if-statement so you only define the function
> if needed:
> 
>      if sys.version_info < (2,4):
>         def sorted(...):
>             ....
> 
> </F> 
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list