4

I'm following the python tutorial here.

I'm trying to define a function with positional only arguments and I get a invalid syntax error

I've tried both the command line interpreter and putting the definition into a file

My definition looks like this

def pos_only(x,/):
    return x**2

I get the usual syntax error

  File "bas.py", line 1
    def pos_ony(x,/):
                  ^
SyntaxError: invalid syntax

Using * to define a keyword argument only function works just fine.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

2 Answers2

1

This works in 3.8 and 3.9, but not before:

$ pythons --file po
below cmd output started 2020 Fri Feb 14 01:57:45 PM PST
/usr/local/cpython-1.0/bin/python (1.0.1) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.1/bin/python (1.1) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.2/bin/python (1.2) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.3/bin/python (1.3) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.4/bin/python (1.4) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.5/bin/python (1.5.2) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.6/bin/python (1.6.1) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.0/bin/python (2.0.1) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.1/bin/python (2.1.0) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.2/bin/python (2.2.0) bad  
      File "<string>", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.3/bin/python (2.3.0) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.4/bin/python (2.4.0) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.5/bin/python (2.5.6) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.6/bin/python (2.6.9) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.7.16/bin/python (2.7.16) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.7/bin/python (2.7.16) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.0/bin/python (3.0.1) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.1/bin/python (3.1.5) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.2/bin/python (3.2.5) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.3/bin/python (3.3.7) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.4/bin/python (3.4.8) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.5/bin/python (3.5.5) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.6/bin/python (3.6.0) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.7/bin/python (3.7.0) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.8/bin/python (3.8.0) good 
/usr/local/cpython-3.9/bin/python (3.9.0a3) good 
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by jnr.posix.JavaLibCHelper (file:/usr/local/jython-2.7/jython.jar) to method sun.nio.ch.SelChImpl.getFD()
WARNING: Please consider reporting this to the maintainers of jnr.posix.JavaLibCHelper
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
/usr/local/jython-2.7/bin/jython (2.7.0) bad  
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by jnr.posix.JavaLibCHelper (file:/usr/local/jython-2.7/jython.jar) to method sun.nio.ch.SelChImpl.getFD()
    WARNING: Please consider reporting this to the maintainers of jnr.posix.JavaLibCHelper
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: mismatched input '/' expecting RPAREN
/usr/local/pypy-5.10.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy-5.3.1/bin/pypy (2.7.10) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy-5.9.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy-6.0.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy-7.0.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy-7.1.1/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy-7.2.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy-7.3.0/bin/pypy (2.7.13) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy3-5.10.0/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy3-5.5.0/bin/pypy3 (3.3.5) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy3-5.8.0-with-lzma-fixes/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy3-5.8.0/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy3-5.9.0/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                      ^
    SyntaxError: invalid syntax
/usr/local/pypy3-6.0.0/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy3-7.0.0/bin/pypy3 (3.5.3) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy3-7.2.0/bin/pypy3 (3.6.9) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/pypy3-7.3.0/bin/pypy3 (3.6.9) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax (expected ')')
/usr/local/micropython-1.11/bin/micropython (3.4.0) bad  
    Traceback (most recent call last):
      File "po", line 3
    SyntaxError: invalid syntax
/usr/local/micropython-git-2017-06-16/bin/micropython (3.4.0) bad  
    Traceback (most recent call last):
      File "po", line 3
    SyntaxError: invalid syntax
/usr/local/micropython-git-2018-06-06/bin/micropython (3.4.0) bad  
    Traceback (most recent call last):
      File "po", line 3
    SyntaxError: invalid syntax
/usr/local/tauthon-2.8/bin/tauthon (2.8.0+) bad  
      File "po", line 3
        def pos_only(x,/):
                       ^
    SyntaxError: invalid syntax
dstromberg
  • 6,954
  • 1
  • 26
  • 27
0

Please read the documentation of Python3.7 https://docs.python.org/3.7/tutorial/controlflow.html

What you said is for new release from Python3.8.

Lane
  • 4,682
  • 1
  • 36
  • 20