今日は Single-dispatch generic functions (PEP-443)。Generic functionは汎用関数と訳されることも多いが、言語によって微妙に違う使われ方をしている。Pythonでは「引数の型によって動作が異なる複数の関数に同じ名前をつける」という意味合いで使われているようだ。 と書いてもさっぱりわからないので、例示。引数の型によって出力が異なる関数を考える。Generic functionsを使わないとこんな感じ。 def print_func(arg): arg_type = type(arg) if arg_type is int: print("found integer:", arg) elif arg_type is list: print("found list:", arg) else: print("found something:"