Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

python - 在Python中创建多行注释的方法?(Way to create multiline comments in Python?)

I have recently started studying Python , but I couldn't find how to implement multi-line comments.

(我最近开始研究Python ,但是找不到如何实现多行注释。)

Most languages have block comment symbols like

(大多数语言都有块注释符号,例如)

/* 

*/

I tried this in Python, but it throws an error, so this probably is not the correct way.

(我在Python中尝试过此方法,但它引发了错误,因此这可能不是正确的方法。)

Does Python actually have a multiline comment feature?

(Python实际上是否具有多行注释功能?)

  ask by Dungeon Hunter translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use triple-quoted strings.

(您可以使用三引号引起来的字符串。)

When they're not a docstring (first thing in a class/function/module), they are ignored.

(如果它们不是文档字符串(类/函数/模块中的第一件事),则将其忽略。)

'''
This is a multiline
comment.
'''

(Make sure to indent the leading ''' appropriately to avoid an IndentationError .)

((确保适当缩进前导'''以避免IndentationError 。))

Guido van Rossum (creator of Python) tweeted this as a "pro tip".

(Guido van Rossum(Python的创建者)在推特上发了一条“专业提示”。)

However, Python's style guide, PEP8, favors using consecutive single-line comments , and this is also what you'll find in many projects.

(但是,Python的样式指南PEP8 倾向于使用连续的单行注释 ,这也是在许多项目中都可以找到的。)

Editors usually have a shortcut to do this easily.

(编辑人员通常都有捷径可以轻松地做到这一点。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...