#【コメント】
# coding【エンコード宣言】
'''【ドキュメンテーション文字列】
pass【処理なし】
assert【診断 (デバッグ)】

#【コメント】

メモ

  • コメントの記述
    • 単一行のみ (単独・コード後方 で使用可)
      コード途中のコメント構文はなし・連続したコメント行は個々に指定
    • バックスラッシュ (\) による継続行には使用不可
    • 構文的なインデントの考慮不要 (インデント量は関係なくエラーなし)
    • コード後方で使用する場合、2個以上のスペース・#・1個のスペース の後にコメントを推奨 (PEP8: Inline Comments)
  • 関連

構文

# コメント (行末まで)

# コメント
value = 123  # コメント

# coding【エンコード宣言】

メモ

構文

1行目か2行目(1行目もコメントのみ)のコメントが、下記の正規表現 (参照:re:正規表現パターン)
coding[=:]\s*([-\w.]+)

下記を推奨
# -*- coding: エンコーディング名 -*-
# vim:fileencoding=エンコーディング名

エンコーディング名下記表参照

エンコーディング名 (一部)

※大文字と小文字の区別なし・ハイフンとアンダースコアの区別なし (別名として有効)
エンコーディング名別名
英語・UTF
ascii646
us-ascii
cp037IBM037
IBM039
cp437437
IBM437
utf-7U7
unicode-1-1-utf-7
utf-8 (デフォルト)U8
UTF
utf8
utf-8-sig
utf-16U16
utf16
utf-16beUTF-16BE
utf-16leUTF-16LE
utf_32U32
utf32
utf_32_beUTF-32BE
utf_32_leUTF-32LE
日本語関連
cp932932
ms932
mskanji
ms-kanji
euc-jis-2004jisx0213
eucjis2004
euc-jisx0213eucjisx0213
euc-jpeucjp
ujis
u-jis
iso-2022-jpcsiso2022jp
iso2022jp
iso-2022-jp
iso-2022-jp-1iso2022jp-1
iso-2022-jp-1
iso-2022-jp-2iso2022jp-2
iso-2022-jp-2
iso-2022-jp-3iso2022jp-3
iso-2022-jp-3
iso-2022-jp-2004iso2022jp-2004
iso-2022-jp-2004
iso-2022-jp-extiso2022jp-ext
iso-2022-jp-ext
shift-jiscsshiftjis
shiftjis
sjis
s_jis
shift-jis-2004shiftjis2004
sjis_2004
sjis2004
shift-jisx0213shiftjisx0213
sjisx0213
s_jisx0213

# -*- coding: shift-jis -*-

'''【ドキュメンテーション文字列】

メモ

構文

"""ドキュメンテーション (複数行可)"""

'''ドキュメンテーション (複数行可)'''

class DocClass:
    """クラスの説明"""

    def method(p1):
        """メソッドの説明

        p1 -- パラメータ1の説明
        """

print("【DocClass.__doc__】")
print(DocClass.__doc__)
print("【DocClass.method.__doc__】")
print(DocClass.method.__doc__)
print("【help(DocClass)】")
help(DocClass)
【出力例】
【DocClass.__doc__】
クラスの説明
【DocClass.method.__doc__】
メソッドの説明

        p1 -- パラメータ1の説明
        
【help(DocClass)】
Help on class DocClass in module __main__:

class DocClass(builtins.object)
 |  クラスの説明
 |  
 |  Methods defined here:
 |  
 |  method(p1)
 |      メソッドの説明
 |      
 |      p1 -- パラメータ1の説明
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

pass【処理なし】

メモ

構文

pass

def new_func():
    pass


class NewClass:
pass

assert【診断 (デバッグ)】

メモ

構文

assert 判定式

下記と等価
if __debug__:
if not 判定式: raise AssertionError

assert 判定式, メッセージ

下記と等価
if __debug__:
if not 判定式: raise AssertionError(メッセージ)

flag = False
assert flag
【出力例】
Traceback (most recent call last):
  File "~.py", line NNN, in <module>
    assert flag
AssertionError
flag = False
assert flag, "メッセージ"
【出力例】
Traceback (most recent call last):
  File "~.py", line NNN, in <module>
    assert flag, "メッセージ"
AssertionError: メッセージ