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
Livid
V2EX  ›  Python

Python 代码如果要在注释中描述一个接口的返回值,是否有什么标准格式?

  •  
  •   Livid · 2018-07-27 21:04:06 +08:00 · 4605 次点击
    这是一个创建于 2101 天前的主题,其中的信息可能已经有所发展或是发生改变。
    16 条回复    2018-09-04 11:44:58 +08:00
    monkeylyf
        1
    monkeylyf  
       2018-07-27 21:12:09 +08:00   ❤️ 1
    可以参考 google 的 python coding style: https://github.com/google/styleguide/blob/gh-pages/pyguide.md#383-functions-and-methods
    以前的公司也用过这个
    “”“ Summary.

    Details.

    :arg1, int: i do this
    :arg2, str: i do that
    :return, list: i do both
    """
    仅供参考
    hlwjia
        2
    hlwjia  
       2018-07-27 21:13:43 +08:00
    现在做的类似于 @monkeylyf 写的,就是标点符号不同,格式一致
    startar
        3
    startar  
       2018-07-27 21:15:48 +08:00 via Android
    那肯定使用标准的 docstring 格式啊
    startar
        4
    startar  
       2018-07-27 21:22:36 +08:00 via Android
    @monkeylyf google 这个其实就是规定使用 python docstring。用这个的话可以让 help()函数显示你的注释,还可以用 sphinx 自动生成文档
    guyskk0x0
        5
    guyskk0x0  
       2018-07-27 21:28:26 +08:00 via Android   ❤️ 1
    1. docstring,主要有 numpy, google, sphinx 三种风格,sphinx 工具支持最完善,个人喜欢 google 风格
    2. 用装饰器 @params @returns 格式自己定,很方便生成其他格式
    3. 类型注解,标准格式( typing )或者自己定格式,也很方便生成其他格式
    est
        6
    est  
       2018-07-27 21:49:01 +08:00
    用 3.6 吧。

    def my_func() -> str: pass
    iyaozhen
        7
    iyaozhen  
       2018-07-27 22:03:32 +08:00 via Android
    2.7 的话好像是
    :rtype:
    xider
        8
    xider  
       2018-07-27 22:15:35 +08:00
    @est 大兄弟没认真审题啊,站长问的是在注释里描述返回值
    silhouette
        9
    silhouette  
       2018-07-27 22:22:50 +08:00 via Android
    @est type hint 有时候会很复杂,尤其是返回一些自定义类型的时候需要导一遍
    PythonAnswer
        10
    PythonAnswer  
       2018-07-27 22:23:08 +08:00
    sphinx 格式 方便生成 api 文档吧
    lihongjie0209
        11
    lihongjie0209  
       2018-07-27 22:39:55 +08:00
    果然动态语言一旦工程化起来, 都在向静态语言靠拢
    Trim21
        12
    Trim21  
       2018-07-27 23:23:01 +08:00
    """

    :return: result
    :rtype: Dict[str, str]
    """
    zhuangzhuang1988
        13
    zhuangzhuang1988  
       2018-07-28 12:07:34 +08:00
    如果用 2 的话 https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html
    不过还是放弃吧, 换语言吧。
    deepreader
        14
    deepreader  
       2018-07-28 12:49:07 +08:00
    typehint, mypy 了解一下
    Sunsgnes
        15
    Sunsgnes  
       2018-07-28 17:41:46 +08:00
    难道是关于刷存在感的公司的事情?
    arthasgxy
        16
    arthasgxy  
       2018-09-04 11:44:58 +08:00
    @Livid
    奇怪, 我之前似乎是在这个帖子下留言“你猜”,然后被你封禁了?
    但我无意中发现换个 ip 竟然能登陆?究竟是没有封号?还是?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5388 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:10 · PVG 16:10 · LAX 01:10 · JFK 04:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.