What Does the Slash Mean in Help() Output

What does the slash mean in help() output?

It signifies the end of the positional only parameters, parameters you cannot use as keyword parameters. Before Python 3.8, such parameters could 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 is now part of the Python language specification, as of version 3.8, see PEP 570 – Python Positional-Only Parameters. Before PEP 570, the syntax was already reserved for possible future inclusion in Python, see PEP 457 - Syntax For Positional-Only Parameters.

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.

What is the meaning of a forward slash / in a Python method signature, as shown by help(foo)?

As explained here, the / as an argument marks the end of arguments that are positional only (see here), i.e. arguments you can't use as keyword parameters. In the case of __eq__(self, value, /) the slash is at the end, which means that all arguments are marked as positional only while in the case of your __init__ only self, i.e. nothing, is positional only.

Edit:
This was previously only used for built-in functions but since Python 3.8, you can use this in your own functions. The natural companion of / is * which allows to mark the beginning of keyword-only arguments. Example using both: 

# a, b are positional-only
# c, d are positional or keyword
# e, f are keyword-only
def f(a, b, /, c, d, *, e, f):
print(a, b, c, d, e, f)

# valid call
f(10, 20, 30, d=40, e=50, f=60)

# invalid calls:
f(10, b=20, c=30, d=40, e=50, f=60) # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60) # e must be a keyword argument

What do * (single star) and / (slash) do as independent parameters?

There is a new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments.[This is new in Python 3.8]

Documentation specifies some of the use cases/benefits of positional-only parameters.

  1. It allows pure Python functions to fully emulate behaviors of
    existing C coded functions. For example, the built-in pow()
    function does not accept keyword arguments:

    def pow(x, y, z=None, /):
    "Emulate the built in pow() function"
    r = x ** y
    return r if z is None else r%z
  2. Another use case is to preclude keyword arguments when the parameter
    name is not helpful. For example, the builtin len() function has
    the signature len(obj, /). This precludes awkward calls such as:

    len(obj='hello')  # The "obj" keyword argument impairs readability
  3. A further benefit of marking a parameter as positional-only is that
    it allows the parameter name to be changed in the future without
    risk of breaking client code. For example, in the statistics module,
    the parameter name dist may be changed in the future. This was made
    possible with the following function specification:

    def quantiles(dist, /, *, n=4, method='exclusive')
    ...

Where as * is used to force the caller to use named arguments. This is one of the use case of named arguments.

So, given the method,

def func(self, param1, param2, /, param3, *, param4, param5):
print(param1, param2, param3, param4, param5)

It must called with

obj.func(10, 20, 30, param4=50, param5=60)

or

obj.func(10, 20, param3=30, param4=50, param5=60)

ie,

  1. param1, param2 must be specified positionally.
  2. param3 can be called either with positional or keyword.
  3. param4 and param5 must be called with keyword argument.

DEMO:

>>> class MyClass(object):
... def func(self, param1, param2, /, param3, *, param4, param5):
... return param1, param2, param3, param4, param5
...
>>> obj = MyClass()
>>>
>>> assert obj.func(10, 20, 30, param4=40, param5=50), obj.func(
... 10, 20, param3=30, param4=40, param5=50
... )

what is the meaning of / in the help function in python 3.5.1

The slash means the end of the "positional only parameters", as better explained in this previous answer.

What does back slash following an equation mark in function argument do in Python

Simple answer is, you don't need the \ at all (in this case**). In python, \ is a line continuation character. All it does is make the line and the following line behave as if it were all on one line. It is only in the code for readability, and it won't cause any problems if you take it out.

**The backslash is not required here because it is a variable initialization. If you were using the backslash for something else such as continuing a string on another line, removing it will raise exceptions.

What is / in the signature of a python function

/ was added in python 3.8 and implies that all the arguments preceding it are positional only and cannot be provided in kwargs method as shown in this document: https://docs.python.org/3/whatsnew/3.8.html#positional-only-parameters

There is a new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments. This is the same notation shown by help() for C functions annotated with Larry Hastings’ Argument Clinic tool.

In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:

def f(a, b, /, c, d, *, e, f):
print(a, b, c, d, e, f)

The following is a valid call:

f(10, 20, 30, d=40, e=50, f=60)

However, these are invalid calls:

f(10, b=20, c=30, d=40, e=50, f=60)   # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60) # e must be a keyword argument


Related Topics



Leave a reply



Submit