Pythonエンジニア育成推進協会は3月2日、「PythonZen & PEP 8 検定試験」を無償で公開開始すると発表した。同試験は、インターネット接続できるブラウザ環境があれば24時間だれでも受験可能だ。3月1日から1カ月間はベータ運営とのことだが、合格者は本認定とされる。

試験ではPythonZenとPEP 8に関する知識を問う問題が20問出題される。全て選択問題であり、正答率70%以上で合格となる。

PythonZen(The Zen of Python)とは、Pythonの設計について記述されたイディオム集だ。「Zen」は日本語の「禅」に由来する。Pythonのコードを記述する際に留意すべき事項が19の格言としてまとめられている。Pythonのインタプリタで、 以下のように、import this と実行すると英文での内容を確認できる。


>>> import this

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

PEP 8とは、Pythonの標準のコーディング規約を規定しているもの。「1レベルインデントするごとに、スペースを4つ使用する」「スペースが好ましいインデント方法である」「1行の長さを最大で79文字までに制限する」などの基本ルールを定める。なお、PEP 8の一貫性にこだわりすぎるのではなく、プロジェクトごとに既存の規約がある場合はそちらを優先するのが良いとのことだ。


# 正しい例:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

# 悪い例:

# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)

# Further indentation required as indentation is not distinguishable.
def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

近年のPython需要の増加に伴ってPythonの関連書籍やプログラミングスクールが増える一方で、Pythonを知らない人が執筆している書籍や、PythonZenやPEP 8を知らない講師が不適切なPython文法を教えているケースがある。こうした状況に対し、Pythonエンジニア育成推進協会では、不適切なPython文法を記述するエンジニアが増える可能性があるとして、PythonZenやPEP 8を学ぶ意識を高めることを目的に試験を無償で提供することにしたのだという。