IndentationError from comment in python

Why does python react to comment indents?

def foo():
    """
    Random comment
    """
    return True

works, but:

def foo():
"""
Random comment
"""
    return True

not working throwing an IndentationError.

It seems strange to me, since comments should not be more than comments. And by the way, this works:

def foo():
# Another random comment
    return True
+5
source share
2 answers

The triplet string is not a comment; This is a method pointer. You can access it using foo.__doc__later, for example, or format for it help(foo). Triple quote ( """or ''') is a python-specific method for specifying a string literal in which string strings should not be escaped.

, . , , , . .

; , . . PEP 257 .

, , # ( ) . , , . . .

+11

, . - , - Python. ( , , .)

# - .

+1

All Articles