Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...
11–20 of 110 posts
Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...
Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
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/
> / 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/ ("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 , "Add support for partial keyword arguments in extension functions"
Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
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)
...Unrelated to the article: the 'x' on the newsletter popup doesn't work on mobile. Makes the article very difficult to read. If the author is reading this, you should fix that!
This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!
This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!
> (...) Although practicality beats purity.
This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!
my favorite cheat is using locals() with string formatting something like this: def example_cheater(color, flavor, age): template = 'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream' return template.format(**locals()) Obviously a contrived example, and it can be argued that using locals() is not very pythonic, but I think it makes the code look much nicer.
>>> matrix = [(1,2,3),(4,5,6),(7,8,9)]
>>> list(zip(*matrix))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Not as readable as a real maths library, but pretty cool and educational.
Think I came across it in Python in a nut shell, by Alex Martelli
Edit: formatting
Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
That 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/9dfa0fe587eae3626ffc9...