Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - 如何在Python中注释掉一段代码[重复](How to comment out a block of code in Python [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

Is there a mechanism to comment out large blocks of Python code?

(有没有一种机制可以注释掉大块的Python代码?)

Right now, the only ways I can see of commenting out code are to either start every line with a # , or to enclose the code in triple quotes: """ .

(现在,我可以看到注释代码的唯一方法是使用#开始每一行,或者用三引号括起代码: """ 。)

The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation.

(这些问题是在每行之前插入#很麻烦, """使得我想用作注释的字符串出现在生成的文档中。)

After reading all comments, the answer seems to be "No".

(阅读完所有评论后,答案似乎是“不”。)

  ask by gbarry translate from so

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

1 Reply

0 votes
by (71.8m points)

Python does not have such a mechanism.

(Python没有这样的机制。)

Prepend a # to each line to block comment.

(在每行前面添加一个#来阻止评论。)

For more information see PEP 8 .

(有关更多信息,请参阅PEP 8 。)

Most Python IDEs support a mechanism to do the block-commenting-with-pound-signs automatically for you.

(大多数Python IDE都支持一种机制来自动为您执行块注释 - 带符号。)

For example, in IDLE on my machine, it's Alt + 3 and Alt + 4 .

(例如,在我的机器上的IDLE中,它是Alt + 3Alt + 4 。)

Don't use triple-quotes;

(不要使用三引号;)

as you discovered, this is for documentation strings not block comments, although it has a similar effect.

(正如您所发现的,这是用于文档字符串而不是阻止注释,尽管它具有类似的效果。)

If you're just commenting things out temporarily, this is fine as a temporary measure.

(如果你只是暂时评论一下,这可以作为临时措施。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...