V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chaleaoch
V2EX  ›  Python

我...问个问题, 正文中的 Python 代码. 为什么可以正确执行?

  •  
  •   chaleaoch · 2020-12-26 19:33:37 +08:00 · 2119 次点击
    这是一个创建于 1215 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def test_func(a, /, b):
        print(a, b)
    
    
    test_func(1, 2)
    

    函数参数中的 / 是个啥? black 和 flake8 都提示异常,但是确实是可以正确执行的. python 3.9

    jdhao
        1
    jdhao  
       2020-12-26 19:36:19 +08:00 via Android
    不懂,没见过,楼下回答
    chaleaoch
        2
    chaleaoch  
    OP
       2020-12-26 19:40:06 +08:00
    查到了. 仅限位置参数.
    Dreax
        3
    Dreax  
       2020-12-26 20:18:01 +08:00
    renmu123
        4
    renmu123  
       2020-12-26 20:19:52 +08:00 via Android
    3.7 还是 3.8 加入的新语法
    abersheeran
        5
    abersheeran  
       2020-12-26 21:30:46 +08:00   ❤️ 3
    / 之前的参数都不能以 name=value 这种形式传参。

    与之类似功能的是 def f(a, *, b): ... * 之后的都必须以 name=value 的形式传参。
    XIVN1987
        6
    XIVN1987  
       2020-12-26 23:33:47 +08:00
    @abersheeran
    前者是 pos-only
    后者是 kw-only
    crayygy
        7
    crayygy  
       2020-12-27 10:48:31 +08:00 via Android
    Python 3.6 3.7 3.8 好像都有一些新增的语法,之前遇到过一次脚本是基于 3.6 写的,结果在 Ubuntu 16.04(预装的 Python 3.5 以下的)就怎么也跑不过,查了以后才知道
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5058 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:39 · PVG 13:39 · LAX 22:39 · JFK 01:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.