问题
What does the / mean in Python 3.4\'s help output for range before the closing parenthesis?
>>> help(range)
Help on class range in module builtins:
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return a virtual sequence of numbers from start to stop by step.
|
| Methods defined here:
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
...
回答1:
It signifies the end of the positional only parameters, parameters you cannot use as keyword parameters. Such parameters can only be specified in the C API.
It means the key argument to __contains__ can only be passed in by position (range(5).__contains__(3)), not as a keyword argument (range(5).__contains__(key=3)), something you can do with positional arguments in pure-python functions.
Also see the Argument Clinic documentation:
To mark all parameters as positional-only in Argument Clinic, add a
/on a line by itself after the last parameter, indented the same as the parameter lines.
and the (very recent addition to) the Python FAQ:
A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally-usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position.
The syntax has also been defined for possible future inclusion in Python, see PEP 457 - Syntax For Positional-Only Parameters.
This PEP has recently been revived and has been accepted for inclusion in Python. With Python 3.8 still in the alpha phase, it may become a reality in that version, or in 3.9, depending on how quickly the reference implementation can be finalised.
Positional-only parameters can lead to cleaner and clearer APIs, make pure-Python implementations of otherwise C-only modules more consistent and easier to maintain, and because positional-only parameters require very little processing, they lead to faster Python code.
回答2:
I asked this question myself. :) Found out that / was originally proposed by Guido in here.
Alternative proposal: how about using '/' ? It's kind of the opposite of '*' which means "keyword argument", and '/' is not a new character.
Then his proposal won.
Heh. If that's true, my '/' proposal wins:
def foo(pos_only, /, pos_or_kw, *, kw_only): ...
I think the very relevant document covering this is PEP 570. Where recap section looks nice.
Recap
The use case will determine which parameters to use in the function definition:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):As guidance:
Use positional-only if names do not matter or have no meaning, and there are only a few arguments which will always be passed in the same order. Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.
If the function ends with /
def foo(p1, p2, /)
This means all functional arguments are positional.
来源:https://stackoverflow.com/questions/24735311/python-what-does-the-slash-mean-in-help-output