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