4 ms·
Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
by tln 8y ago
Whats with the / in the help for sorted?
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
- dec0dedab0de 8y agoThat is curious, it looks like it might just be a type-o in the documentation. Since it is written in C and not python, it is not automatically generated. But I don't know enough C to say for sure, maybe someone else can. https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc973680c6a17f35de3864/Python/bltinmodule.c#L2203 https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...
- jnwatson 8y agoNope. It represents positional-only arguments. You can have positional-only arguments for C function but not Python functions. Just a weird Python wart.
- eesmith 8y agoIt looks like it's a semi-internal(?) API notation for position-only fields. That is, you cannot say sorted(iterable=range(10)) because 'iterable' is a position-only parameter. Some pointers. https://www.python.org/dev/peps/pep-0436/ https://www.python.org/dev/peps/pep-0436/ > / Establishes that all the proceeding arguments are positional-only. For now, Argument Clinic does not support functions with both positional-only and non-positional-only arguments. ... (The semantics of / follow a syntax for positional-only parameters in Python once proposed by Guido. [5] ) https://www.python.org/dev/peps/pep-0457/ https://www.python.org/dev/peps/pep-0457/ ("PEP 457 -- Syntax For Positional-Only Parameters" - draft) > All parameters before the / are positional-only. If / is not specified in a function signature, that function does not accept any positional-only parameters. This is only for documenting API signatures. There is no way to implement directly in Python code. Eg, that PEP goes on to say: > This PEP does not propose we implement positional-only parameters in Python. The goal of this PEP is simply to define the syntax, so that: > - Documentation can clearly, unambiguously, and consistently express exactly how the arguments for a function will be interpreted. > - The syntax is reserved for future use, in case the community decides someday to add positional-only parameters to the language. See also https://bugs.python.org/issue21314 https://bugs.python.org/issue21314 , "Add support for partial keyword arguments in extension functions"
- joshuamorton 8y agoPython has a bit of a wart in that arguments can normally be passed as keyword or positional. Python3 also added the ability to pass keyword only args (the ones after the `* `), That is def f(a, b): return a+b can be called as `f(1, 2)` or `f(b=2, a=1)`, although I'd look at you funny if you did the second. Certain functions implemented in C also have positional only args, so the function of signature weird(p_only, /, pos, kw=None, * , kw_only=None) can be called in the following ways (1 is p_only, 2, is pos, 3 is kw, 4 is kw_only) weird(1, 2, 3) weird(1, 2, 3, kw_only=4) weird(1, pos=2, kw=3) weird(1, pos=2) weird(1, 2, kw_only=4) ... but not weird(p_only=1, 2) weird(1,2,3,4) ...
- masklinn 8y agoIt's a documentation-only (currently, I remember seeing posts on python-dev to add it as a python-level construct but I don't believe that's been implemented) to document positional-only parameters, a feature currently only available to "native" functions. Basically, at the C level you can define positional-only parameters such that sorted can not be called as `sorted(iterable=xxx)`, and this was specified using a "/" in argument clinic (https://www.python.org/dev/peps/pep-0436/#special-syntax-for-parameter-lines https://www.python.org/dev/peps/pep-0436/#special-syntax-for...) and now appears in some docstrings. This is pretty common in builtins e.g. you can't give a name to the first argument of `max`, it doesn't have one (except informally). Anything before the / is positional-only (the name is informational but not definitional), between / and * it's both positional and keyword, and after * (or *args) it's keyword-only.