21

In the Python 3.8 Programming FAQ, I saw the following function definition:

class callByRef:
    def __init__(self, /, **args):
        for key, value in args.items():
            setattr(self, key, value)

This is missing in the Python 3.7 version:

class callByRef:
    def __init__(self, **args):
        for (key, value) in args.items():
            setattr(self, key, value)

What is this new / syntax?

How does it relate to a / appearing in help() output?


Note: this and this question are about help() annotation, whereas this question is about new syntax and any differences to the help() annotation.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • Related: [Bare asterisk in function arguments?](https://stackoverflow.com/q/14301967/5353461) – Tom Hale Jun 09 '19 at 11:51
  • 4
    People don't read carefully. It is not a dupe. The voting is really strange. – sanyassh Jun 09 '19 at 12:28
  • @Sanyash 1) both posts describe the same behavior, so there is little value in this post besides saying "and now it's semantically enforced as well" and 2) python3.8 is still under active development, and won't release a RC until Oktober. So talking about its status (rather than the PEPs) is a bad idea – Arne Jun 09 '19 at 12:52
  • 4
    @Arne I disagree. 1) we can't write pure Python functions with `/` before 3.8. It was only C API feature. Now we can. I can't say it is the same behavior. It is a big change. 2) This post has much more value than any of the "gimme teh codez" questions, "debug my code" questions, etc. 3) imo not a bad idea. This question is not hypothetical. – sanyassh Jun 09 '19 at 13:04
  • @Sanyash They still describe the same behavior.. that's what I said. And the dupes are fine, in particular [this one](https://stackoverflow.com/questions/24735311/python-what-does-the-slash-mean-in-help-output) is really good. And I never said that this question is hypothetical, I said it's a bad idea to discuss a feature that isn't even released. In particular in this QA form. – Arne Jun 09 '19 at 13:12

1 Answers1

25

Introduction as syntax

The / as syntax was introduced in Python 3.8.

The rationale for / in an argument list is given in PEP 570 -- Python Positional-Only Parameters:

The new syntax will enable library authors to further control how their API can be called. It will allow designating which parameters must be called as positional-only, while preventing them from being called as keyword arguments.

Previously, (informational) PEP 457 defined the syntax, but with a much more vague scope. This PEP takes the original proposal a step further by justifying the syntax and providing an implementation for the / syntax in function definitions.

Comparison of syntax and annotational PEPs

Similarities

For all intents and purposes, if you understand help()'s / notation, then that's what is formally included as Python syntax in v3.8 via PEP 570.

Differences

  • PEP 570 -- Python Positional-Only Parameters

    • Defines the syntax of Python 3.8+
    • Formal grammatical specification of the syntax
    • Type: Accepted
  • PEP 457 -- Notation For Positional-Only Parameters

    • Defines notation (not syntax) used in help() annotations
    • Informal English language description
    • Type: Informational

Explanation and example

There are already excellent answers on the meaning and usage of / in arguments.

To save you the click through:

A / means that all preceding parameters are positional-only parameters. Positional-only parameters before a / cannot be passed as name=value when calling the function.

Python 3.8 What's New gives the following example:

def pow(x, y, z=None, /):
    r = x**y
    if z is not None:
        r %= z
    return r

Valid function calls:

  • pow(2, 10)
  • pow(2, 10, 17)

Invalid function calls:

  • pow(x=2, y=10)
  • pow(2, 10, z=17)
Community
  • 1
  • 1
Tom Hale
  • 40,825
  • 36
  • 187
  • 242