3

我在这里关注 python 教程。

我正在尝试使用仅位置参数定义一个函数,但我得到一个无效的语法错误

我已经尝试了命令行解释器并将定义放入文件中

我的定义看起来像这样

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

我得到通常的语法错误

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

使用*仅定义关键字参数的函数就可以了。

4

2 回答 2

1

这在 3.8 和 3.9 中有效,但在此之前无效:

$ 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
于 2020-02-14T22:00:12.623 回答
0

请阅读 Python3.7 的文档https://docs.python.org/3.7/tutorial/controlflow.html

你说的是Python3.8的新版本。

于 2020-02-14T21:40:21.630 回答