Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python use unconventional triple-quotation marks for comments?

Why didn't python just use the traditional style of comments like C/C++/Java uses:

/**  * Comment lines   * More comment lines  */  // line comments // line comments // 

Is there a specific reason for this or is it just arbitrary?

like image 773
mmtauqir Avatar asked Aug 14 '11 14:08

mmtauqir


People also ask

Why do we use triple quotes in Python?

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. The syntax for triple quotes consists of three consecutive single or double quotes.

Can Triple quotes be used as comments in Python?

Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code. Special characters like TABs, verbatim or NEWLINEs can also be used within the triple quotes.

What does triple apostrophe mean in Python?

Enclose strings containing both single and double quotes such that no escaping is needed. Enclose multi-line strings.

What does 3 quotation marks mean?

The triple quotation is a nice way to be able to include other types of quotation within your string without having to use escape characters. For example: print("He said \"my name's John\"") That example requires escape characters \" to use double quote marks.


1 Answers

Python doesn't use triple quotation marks for comments. Comments use the hash (a.k.a. pound) character:

# this is a comment 

The triple quote thing is a doc string, and, unlike a comment, is actually available as a real string to the program:

>>> def bla(): ...     """Print the answer""" ...     print 42 ... >>> bla.__doc__ 'Print the answer' >>> help(bla) Help on function bla in module __main__:  bla()     Print the answer 

It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention (and has the advantage of being multiline).

like image 86
balpha Avatar answered Sep 17 '22 19:09

balpha