how does not work nested comment in python?

Vlastimil Brom vlastimil.brom at gmail.com
Wed Sep 4 03:55:19 EDT 2013


2013/9/4  <vnkumbhani at gmail.com>:
> example:
>
> print "hello" # print comment +"world"    => single line comment
> print "hello" '''print comment''' +"world"   => multiple line comment
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
python only has single line comments, which apply from a "#" to the
end of the respective line.
There are some possibilities/workarounds/hacks for "commenting out",
i.e. (temporarily) disabling, parts of the code
if False:
    <indented original code>

Sometimes triple-quoted multiline strings are (mis)used this way
"""<original code
in multiple
lines>"""

which actually converts the code to a not accessible multiline string.
However, triple quoting is not an official means for multiline comments.
What you are seeing in your second example is implicit string
concatenation (which works regardless of the type of the quotes) -
adjacent string literals in the code are joined automatically:
>>> "abc" 'def' """ghi""" '''jkl'''
'abcdefghijkl'
>>>

hth,
  vbr



More information about the Python-list mailing list