2015-03-25 python> Multi-line comment> Watch out for indent

Operation check

CentOS 6.5
Python 2.6.6

In Python, it seems that you can use " "" or''' for multi-line comments. Reference

def main():
    print "test start"
    """
    test1
    """

"""
test2
"""
    print "test end"

The first thing I didn't understand was that the comment part of test2 gave me the error ʻIndentation Error: unexpected indent`.

Since python interprets the structure by indent, the comment of test2 was interpreted as "not the definition in main ()" because there is no indent, and an error occurred.

If you do the following, the error will disappear.

def main():
    print "test start"
    """
    test1
    """

    """
    test2
    """
    print "test end"

In other words, when writing a multi-line comment, it seems that you need to write it with the same indent as the previous line. This happens for multi-line comments, and single-line comments starting with # can be written from the first digit without worrying about indents.

def main():
    print "test start"
    """
    test1
    """

#This comment is O.K.
    """
    test2
    """
    print "test end"

Recommended Posts

2015-03-25 python> Multi-line comment> Watch out for indent
python comment out
Watch out for randint
# 2 [python3] Separation and comment out
Things to watch out for when using default arguments in Python
Comment out
2016-10-30 else for Python3> for:
python [for myself]
Watch out for the return value of __len__
Watch out for global variables outside of lambda_handler (datetime)
Things to watch out for when migrating with Django
About Python for loops
Python basics â‘¡ for statement
[Python] Multi-line string assignment
About Python, for ~ (range)
python textbook for beginners
Refactoring tools for Python
Indent comprehensions in Python
python for android Toolchain
OpenCV for Python beginners
Install Python (for Windows)
[Python] for statement error
Python environment for projects