有没有办法在多行代码中添加注释? [英] Is there a way to put comments in multiline code?

查看:88
本文介绍了有没有办法在多行代码中添加注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不起作用:

something = \
    line_of_code * \    #  Comment
    another_line_of_code * \    #  Comment
    and_another_one * \         #  Comment
    etc

这两者都没有:

something = \
    #  Comment \
    line_of_code * \
    #  Comment \
    another_line_of_code * ...

这两者都没有:

something = \
    ''' Comment ''' \
    line_of_code * \
    ''' Comment ''' \
    another_line_of_code * ...

有没有办法将代码中的注释分成多行?

Is there a way to make comments in the code broken into multiple lines?

推荐答案

这样做:

a, b, c, d = range(1, 5)

result = (
    # First is 1
    a *
    # Then goes 2, result is 2 now
    b *
    # And then 3, result is 6
    c *
    # And 4, result should be 24
    d
)

实际上,根据 PEP8 括号比斜杠更受欢迎,当将某些内容分成多行:

Actually, according to PEP8 parentheses are preferred over slashes, when breaking something into multiple lines:

包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行继续符.通过将表达式括在括号中,可以将长行分成多行.这些应该优先于使用反斜杠作为行继续符.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

就您而言,它还允许发表评论.

In your case it also allows to put comments.

这是一个证明,它有效:http://ideone.com/FlccUJ

Here is a proof, that it works: http://ideone.com/FlccUJ

这篇关于有没有办法在多行代码中添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆