Comments of embedded triple quotes

In python, for commenting over multiple lines we use triple quotes

def x(): """This code will add 1 and 1 """ a=1+1 

but what if I have to comment on a block of code that already contains many other comment blocks (triple quotes). For example, if I want to fully comment on this feature.

 """ def x(): """This code will add 1 and 1 """ a=1+1 """ 

This does not work. How can I comment on such blocks of code.

+4
source share
4 answers

In python, we use triple commas to comment out multiple lines.

This is just one way to do this and you are technically using a string literal, not a comment. And, despite the fact that it has become quite durable, this way of writing comments has the drawback that you observed: you cannot comment on nested blocks. 1

Python has no nested multi-line comments, it's that simple. If you want to comment on multiple lines that allow nested comments, the only safe choice is to comment on each line.

Most editors have a command that simplifies commenting or a few lines.


1 For one level of nesting, you can use '''"""nested """''' or vice versa. But I would not recommend it.

+10
source

What I often do in short hacking and firing situations looks something like this. This is not a comment, and it does not cover all cases (because you need a block), but perhaps it is useful:

 if 0: # disabled because *some convincing reason* def x(): """This code will add 1 and 1 """ a=1+1 

Or, if you cannot or do not want to enter indentation levels between typical ones:

 # disabled because *some convincing reason* if 0: # def x(): """This code will add 1 and 1 """ a=1+1 
+4
source

You should use # to comment at the beginning of each line. It is very simple if you use eclipse + pydev.

Just select the code block for comments and press Ctrl + \ . The same goes for uncommentng.

I am sure that there are such simple ways in other editors.

0
source

I am taking a python Udacity programming course by creating a search engine. They use triple quotation marks to enclose the source code of a web page as a string in the "page" variable to search for all links.

page = '' 'webpage source code' '', which is searched using page.find ()

-1
source

Source: https://habr.com/ru/post/1413503/


All Articles