IndentationError from comment in python

前端 未结 2 1800
走了就别回头了
走了就别回头了 2021-02-15 18:12

How come python reacts to indentations of a comment?

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

works, but:

         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-15 18:32

    The triple-quoted string is not a comment, it is a string literal. It's not assigned or used in any way by your code, but it's still a regular string and has to fit the syntax of Python. (In this case it happens to be the docstring, but that has nothing to do with whether indentation matters for it or not.)

    # is the way to get comments.

提交回复
热议问题