推荐学习书目
› 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
403.83D
551.06D
V2EX  ›  Python

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

  •  
  •   Livid ·
    PRO
    · Jul 27, 2018 · 5847 views
    This topic created in 3027 days ago, the information mentioned may be changed or developed.
    16 replies  •  2018-09-04 11:44:58 +08:00
    monkeylyf
    monkeylyf
    Jul 27, 2018❤️1
    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
    hlwjia
    Jul 27, 2018
    2
    现在做的类似于 @monkeylyf 写的,就是标点符号不同,格式一致
    startar
    startar
    Jul 27, 2018 via Android
    3
    那肯定使用标准的 docstring 格式啊
    startar
    startar
    Jul 27, 2018 via Android
    4
    @monkeylyf google 这个其实就是规定使用 python docstring。用这个的话可以让 help()函数显示你的注释,还可以用 sphinx 自动生成文档
    guyskk0x0
    guyskk0x0
    Jul 27, 2018 via Android❤️1
    5
    1. docstring,主要有 numpy, google, sphinx 三种风格,sphinx 工具支持最完善,个人喜欢 google 风格
    2. 用装饰器 @params @returns 格式自己定,很方便生成其他格式
    3. 类型注解,标准格式( typing )或者自己定格式,也很方便生成其他格式
    est
    est
    Jul 27, 2018
    6
    用 3.6 吧。

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

    :return: result
    :rtype: Dict[str, str]
    """
    zhuangzhuang1988
    zhuangzhuang1988
    Jul 28, 2018
    13
    如果用 2 的话 https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html
    不过还是放弃吧, 换语言吧。
    deepreader
    deepreader
    Jul 28, 2018
    14
    typehint, mypy 了解一下
    Sunsgnes
    Sunsgnes
    Jul 28, 2018
    15
    难道是关于刷存在感的公司的事情?
    arthasgxy
    arthasgxy
    Sep 4, 2018
    16
    @Livid
    奇怪, 我之前似乎是在这个帖子下留言“你猜”,然后被你封禁了?
    但我无意中发现换个 ip 竟然能登陆?究竟是没有封号?还是?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4746 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 03:07 · PVG 11:07 · LAX 19:07 · JFK 22:07
    ♥ Do have faith in what you're doing.