有没有一种方法可以在Python中创建多行注释? [英] Is there a way to create multiline comments in Python?

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

问题描述

我最近开始研究 Python ,但是我找不到方法实施多行注释。大多数语言都有块注释符号,例如

I have recently started studying Python, but I couldn't find how to implement multi-line comments. Most languages have block comment symbols like

/*

*/

我在Python中尝试过此方法,但它会引发错误,因此这可能不是正确的方法。 Python实际上是否具有多行注释功能?

I tried this in Python, but it throws an error, so this probably is not the correct way. Does Python actually have a multiline comment feature?

推荐答案

您可以使用三引号字符串。当它们不是文档字符串时(类/函数/模块中的第一件事),它们将被忽略。

You can use triple-quoted strings. When they're not a docstring (the first thing in a class/function/module), they are ignored.

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

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

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

Guido van Rossum (Python的创建者)在Twitter上发了一条作为专业提示。

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

不过,Python的样式指南,PEP8,喜欢使用连续的单行注释,这也是您在许多项目中都能找到的。文本编辑器通常都有一个快捷方式可以轻松地做到这一点。

However, Python's style guide, PEP8, favors using consecutive single-line comments, and this is also what you'll find in many projects. Text editors usually have a shortcut to do this easily.

这篇关于有没有一种方法可以在Python中创建多行注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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