LoginSignup
46
44

More than 3 years have passed since last update.

2015-03-25 python > 複数行のコメント > indentに気をつける

Last updated at Posted at 2015-03-25

動作確認

CentOS 6.5
Python 2.6.6

Pythonでは複数行コメントには"""'''を使えばよいようだ
参考

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

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

最初よく理解できなかったのは、test2のコメント部分でIndentationError: unexpected indentというエラーが出てしまうこと。

pythonはindentにより構造を解釈するので、test2のコメントはindentがないために「main()内の定義ではない」と解釈されエラーが出ていた。

以下のようにするとエラーが出なくなる。

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

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

つまりは、複数行コメントを書くときは前の行と同じindentで書く必要があるようだ。
これは複数行コメントに関して発生することで、#で始まる1行コメントはindentを気にせず1桁目から書ける。

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

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
44