【Python】PythonZen(The Zen of Python)

■ はじめに

https://pythonzen-pep8-exam.jp/
https://www.pythonic-exam.com/archives/news/python-zen

で「PythonZen & PEP 8 検定試験」って
無料で受けられるPython資格試験をうけてみた。
(試験については、以下の関連記事を参照のこと)

https://dk521123.hatenablog.com/entry/2022/03/29/225946

その試験で、PythonZen(The Zen of Python)って、
Pythonの格言的なものを学んだので、メモ。

目次

【0】PythonZen(The Zen of Python)
 1)Zen of Pythonの全文表示できるコマンド
【1】Beautiful is better than ugly.
【2】Explicit is better than implicit.
【3】Simple is better than complex.
【4】Complex is better than complicated.
【5】Flat is better than nested.
【6】Sparse is better than dense.
【7】Readability counts.
【8】Special cases aren't special enough to break the rules.
【9】Although practicality beats purity.
【10】Errors should never pass silently.
【11】Unless explicitly silenced.
【12】In the face of ambiguity, refuse the temptation to guess.
【13】There should be one-- and preferably only one --obvious way to do it.
【14】Although that way may not be obvious at first unless you're Dutch.
【15】Now is better than never.
【16】Although never is often better than *right* now.
【17】If the implementation is hard to explain, it's a bad idea.
【18】If the implementation is easy to explain, it may be a good idea.
【19】Namespaces are one honking great idea -- let's do more of those!

【0】PythonZen(The Zen of Python

* Pythonの心得を纏めた格言集
 => Zen は、日本の「禅」

https://www.python.org/dev/peps/pep-0020/

1)Zen of Pythonの全文表示できるコマンド

# Pythonインタプリタ を起動
python

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
... (っとありがたいお言葉が表示される)

【1】Beautiful is better than ugly

訳:醜いより美しいほうがいい

【2】Explicit is better than implicit.

訳:暗示するより明示するほうがいい

【3】Simple is better than complex.

訳:複雑よりはシンプルなほうがいい

【4】Complex is better than complicated.

(【3】の続きとして)
訳:ややっこしくなるよりは複雑であるほうがまし

# この文、訳すのムズイ。以下の事例を参照。

* 難しい処理を書く場合は、コードの行数が増えてしまうこともある。
* 難しいアルゴリズムを書く場合でも、読みやすいコードを目指すべきである。
* 条件分岐が多いという理由だけでは、コードがcomplexであるとは限らない。

【5】Flat is better than nested.

訳:ネストされたものよりフラットな方がいい

【6】Sparse is better than dense.

訳:密よりスペースが空いている方がいい

NG : a=b
OK : a = b

【7】Readability counts.

訳:読み易いことは価値がある

【8】Special cases aren't special enough to break the rules.

訳:特殊なケースはルールを破ってしまうほど特別じゃない
 => 特殊だからってルールを破っちゃダメ

cf. A enough to B = Aなので、Bする

【9】Although practicality beats purity.

(【8】を受けて)
訳:しかし、実用性は、純粋を受け破る

cf. practicality = 実用性
cf. purity = 純粋

【10】Errors should never pass silently.

訳:エラーは決して黙って見逃さないほうがいい

【11】Unless explicitly silenced.

(【10】をうけて)
訳:ただし、明示的に無視している場合は除く

cf. explicitly  = 明示的に

【12】In the face of ambiguity, refuse the temptation to guess.

訳:あいまいな場面に出くわした場合、推測する誘惑を拒否せよ

cf. ambiguity = あいまい
cf. temptation = 誘惑

【13】There should be one-- and preferably only one --obvious way to do it.

訳:一つある、
 -- 好ましいただ唯一の、
 -- それをやる明らかなやり方が

【14】Although that way may not be obvious at first unless you're Dutch.

(【13】を受けて)
訳:しかし、そのやり方は最初は明らかではないかもしれない
 もし、オランダ人でなければ

【15】Now is better than never.

訳:やらないより、今やった方がいい

* バグを放置せずに今すぐ修正する
* 無限ループは終わらせるべき

【16】Although never is often better than right now.

訳:ただ、今"すぐ"にやるより、やらないほうがいいことが多い

* YAGNI(You ain't gonna need it.)
 => 機能は実際に必要となるまでは追加しない方がよい
 => YAGNI については、以下の関連記事も参照のこと。

良いプログラムを書くために ~ 有名な格言 ~
https://dk521123.hatenablog.com/entry/2014/09/20/160915

【17】If the implementation is hard to explain, it's a bad idea.

訳:実装の説明が難しいなら、それは悪い考えだ。

【18】If the implementation is easy to explain, it may be a good idea.

(【17】を受けて)
訳:実装の説明が容易いなら、それはいいアイデアかも。

【19】Namespaces are one honking great idea -- let's do more of those!

訳:名前空間は、とても素晴らしいアイデアである -- 積極的に使いましょ!

cf. honking = でっかい

参考文献

https://qiita.com/IshitaTakeshi/items/e4145921c8dbf7ba57ef

関連記事

良いプログラムを書くために ~ 有名な格言 ~
https://dk521123.hatenablog.com/entry/2014/09/20/160915
PythonZen & PEP 8 検定試験
https://dk521123.hatenablog.com/entry/2022/03/29/225946