Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In the signature returned interactively by help(foo), what is the meaning of a /?

In [37]: help(object.__eq__)  Help on wrapper_descriptor:  __eq__(self, value, /)     Return self==value.  In [55]: help(object.__init__)  Help on wrapper_descriptor:  __init__(self, /, *args, **kwargs)     Initialize self.  See help(type(self)) for accurate signature. 

I thought it might be related to keyword-only arguments, but it's not. When I create my own function with keyword-only arguments, positional and keyword-only arguments are separated by * (as expected), not by /. What does the / mean?

like image 580
gerrit Avatar asked Jan 30 '15 20:01

gerrit


People also ask

What does the forward slash mean in Python?

The double forward slash in Python is known as the integer division operator. Essentially, it will divide the left by the right, and only keep the whole number component.

What is the forward slash symbol?

The forward slash (or simply slash) character (/) is the divide symbol in programming and on calculator keyboards. For example, 10 / 7 means 10 divided by 7. The slash is also often used in command line syntax to indicate a switch.

How do you use a forward slash in a string in Python?

Use the str. split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .

What's the difference between a forward slash and a backslash?

Summary: The Backslash and Forward SlashThe backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.


1 Answers

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 
like image 107
runDOSrun Avatar answered Oct 11 '22 11:10

runDOSrun