Why sorted () parameter requires a keyword argument

If you verify the signature of an inline function sorted()of a Python function as follows:

import inspect
print(inspect.signature(sorted))

Signature: (iterable, key=None, reverse=False).

Based on my understanding of the positional and optional arguments obtained here , it would seem that you could provide an argument iterable, and then a keywithout using a keyword key=for the argument key. But when passing an argument, keyyou always need to specify key=. Why is this?

I understand that if you want to specify reverse=True, but without an argument key, you will need a keyword for this, but I do not understand why you need to indicate key=when you are.

+4
source share
1 answer

This is Python issue 26729 , an error in sorted.__text_signature__which is missing /and *necessary to indicate that it iterableis only positional, but keyalso reversehave a keyword. The patch is in a review of the patch assigned to Raymond Hettinger. As soon as the revised version is released, the signature should appear as

(iterable, /, *, key=None, reverse=False)
+2
source

Source: https://habr.com/ru/post/1648426/


All Articles