开发者

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

开发者 https://www.devze.com 2023-03-28 05:01 出处:网络
Why didn\'t python just use the traditional st开发者_运维问答yle of comments like C/C++/Java uses:

Why didn't python just use the traditional st开发者_运维问答yle 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?


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).


A number of the answers got many of the points, but don't give the complete view of how things work. To summarize...

# comment is how Python does actual comments (similar to bash, and some other languages). Python only has "to the end of the line" comments, it has no explicit multi-line comment wrapper (as opposed to javascript's /* .. */). Most Python IDEs let you select-and-comment a block at a time, this is how many people handle that situation.

Then there are normal single-line python strings: They can use ' or " quotation marks (eg 'foo' "bar"). The main limitation with these is that they don't wrap across multiple lines. That's what multiline-strings are for: These are strings surrounded by triple single or double quotes (''' or """) and are terminated only when a matching unescaped terminator is found. They can go on for as many lines as needed, and include all intervening whitespace.

Either of these two string types define a completely normal string object. They can be assigned a variable name, have operators applied to them, etc. Once parsed, there are no differences between any of the formats. However, there are two special cases based on where the string is and how it's used...

First, if a string just written down, with no additional operations applied, and not assigned to a variable, what happens to it? When the code executes, the bare string is basically discarded. So people have found it convenient to comment out large bits of python code using multi-line strings (providing you escape any internal multi-line strings). This isn't that common, or semantically correct, but it is allowed.

The second use is that any such bare strings which follow immediately after a def Foo(), class Foo(), or the start of a module, are treated as string containing documentation for that object, and stored in the __doc__ attribute of the object. This is the most common case where strings can seem like they are a "comment". The difference is that they are performing an active role as part of the parsed code, being stored in __doc__... and unlike a comment, they can be read at runtime.


Triple-quotes aren't comments. They're string literals that span multiple lines and include those line breaks in the resulting string. This allows you to use

somestr = """This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is\
 significant."""

instead of

somestr = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."


Most scripting languages use # as a comment marker so to skip automatically the shebang (#!) which specifies to the program loader the interpreter to run (like in #!/bin/bash). Alternatively, the interpreter could be instructed to automatically skip the first line, but it's way more convenient just to define # as comment marker and that's it, so it's skipped as a consequence.


Guido - the creator of Python, actually weighs in on the topic here: https://twitter.com/gvanrossum/status/112670605505077248?lang=en

In summary - for multiline comments, just use triple quotes. For academic purposes - yes it technically is a string, but it gets ignored because it is never used or assigned to a variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消