端末に出力されたZen of Python
Zen of Python とは、Python プログラミング言語 に影響を与えたコンピュータプログラム を書くための19の「原則」 を集めたものである[ 1] 。これらの原則に沿ったPythonのコードはしばしば「Pythonic」と呼ばれる[ 2] 。
ソフトウェア工学者 のティム・ピーターズ (英語版 ) がこの原則集を書き、1999年にPythonのメーリングリスト に投稿した[ 3] 。ピーターズによる原則集では、Python言語の原作者のグイド・ヴァンロッサム に言及した「グイドが埋める」とされた20番目の原則が空のまま残されていた。20番目の原則は空のまま埋められていない。
ピーターズによるZen of Pythonは20番目の公式のPython Enhancement Proposal (PEP)として文書化され、パブリックドメイン で公開された[ 4] 。また、Zen of PythonはPythonインタプリタ にイースターエッグ として含まれており、import this
と入力することで表示できる[ 1] [ 4] [ 注釈 1] 。
2020年5月、GNU Mailman の開発者のBarry Warsawが歌の歌詞としてZen of Pythonを使用した[ 5] [ 6] 。
イラストで表現されたZen of Python
原則は以下の通りである[ 注釈 2] :
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.[ 注釈 3]
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.[ 注釈 4]
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!
原則の1つである「There should be one-- and preferably only one --obvious way to do it」は「Pythonic」な方法と言うことができる[ 8] 。「Pythonic」の公式な定義は以下の通りである[ 2] :
他の言語で一般的な考え方で書かれたコードではなく、Python の特に一般的なイディオムに従った考え方やコード片。例えば、Python の一般的なイディオムでは for
文を使ってイテラブルのすべての要素に渡ってループします。他の多くの言語にはこの仕組みはないので、Python に慣れていない人は代わりに数値のカウンターを使うかもしれません:
for i in range ( len ( food )):
print ( food [ i ])
これに対し、きれいな Pythonic な方法は:
for piece in food :
print ( piece )
理解するのが難しいコードや他のプログラミング言語から大まかに転写したように読めるコードは「unpythonic」と呼ばれている[ 9] 。
Zen of Pythonの公開以来、その効果と開発者の間での実際の使用に関する調査が行われてきた。初心者と経験豊富なPythonプログラマーの間の解釈の違いにも関わらず、様々な能力を持つ13人のPythonプログラマーに対するインタビューでは、Zen of Pythonが「開発者がコードを書いたりコードについて話したりする方法に良い影響を与えている」ことが示されている[ 8] 。研究者はこのケーススタディーを拡張してGitHub リポジトリでのPythonイディオムの使用を調査し、「Pythonicイディオム」[ 注釈 5] の使用量が時間の経過とともに増加していることを発見した[ 10] 。Zen of Pythonに沿ってPythonコードを記述すると、Pythonプログラムのメモリ 使用量と実行時間 を節約できる可能性がある[ 11] 。Pythonicなコードを書きたいという要求から、プログラマーがこの目標を達成できるようにするためのリファクタリングツールが開発された[ 12] [ 13] 。
^ Zen of Pythonのソースコードはthis.py - GitHub で確認できる。
^ 以下のWikipediaのリンクはさらなる参照と理解のために追加されたものであり、Zen of Pythonでは明示的にリンクされていない。
^ この原則と最後の原則のダッシュ による書式設定は、様々な書式設定規則を考慮して意図的に一貫性をなくしている[ 7] 。
^ インタプリタのイースターエッグではこの原則は「Although never is often better than *right* now.」と表示される。これは一般的な書式設定機能がしばしば不可能なことがあるプレーンテキスト 通信の長年の慣習に従っており、強調 (英語版 ) はアスタリスクで表現される。
^ 彼らのPythonicイディオムはslimshadyiam .github .io /ZenYourPython / にある。
^ a b Reitz, Kenneth (2011–2019). “Code Style ”. The Hitchhiker’s Guide to Python . 2019年3月26日閲覧。
^ a b “用語集 ”. Python documentation . Python Software Foundation. 2025年3月22日時点のオリジナルよりアーカイブ 。2025年3月22日閲覧。
^ Peters, Tim (1999年6月4日). “The Python Way ”. Python Software Foundation . 2019年3月26日閲覧。
^ a b Peters, Tim (2004年8月19日). “PEP 20—The Zen of Python ”. Python Software Foundation . 2019年3月26日閲覧。
^ Warsaw, Barry (2020年5月10日). “The Zen of Python ”. We Fear Change . 2020年6月3日時点のオリジナルよりアーカイブ 。2025年3月22日閲覧。
^ Warsaw, Barry (23 May 2020). The Zen of Python . YouTube . The Zbwedicon. 2021年12月11日時点のオリジナルよりアーカイブ 。 Archived 2020-06-03 at the Wayback Machine .
^ “Issue 3364: An ortographical typo in Zen of Python text - Python tracker ”. 2021年2月10日閲覧。
^ a b Alexandru, Carol V.; Merchante, José J.; Panichella, Sebastiano; Proksch, Sebastian; Gall, Harald C.; Robles, Gregorio (2018-10-24). “On the usage of pythonic idioms” . Proceedings of the 2018 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software . Onward! 2018. New York, NY, USA: Association for Computing Machinery. pp. 1–11. doi :10.1145/3276954.3276960 . ISBN 978-1-4503-6031-9 . OCLC 1362712424 . オリジナル の2018-10-03時点におけるアーカイブ。. https://doi.org/10.1145/3276954.3276960
^ “Code Style – The Hitchhiker's Guide to Python ”. docs.python-guide.org . 2021年1月27日時点のオリジナルよりアーカイブ 。2021年1月20日閲覧。
^ Farooq, Aamir; Zaytsev, Vadim (2021-11-22). “There is more than one way to zen your Python” . Proceedings of the 14th ACM SIGPLAN International Conference on Software Language Engineering . SLE 2021. New York, NY, USA: Association for Computing Machinery. pp. 68–82. doi :10.1145/3486608.3486909 . ISBN 978-1-4503-9111-5 . https://dl.acm.org/doi/10.1145/3486608.3486909
^ Leelaprute, Pattara; Chinthanet, Bodin; Wattanakriengkrai, Supatsara; Kula, Raula Gaikovina; Jaisri, Pongchai; Ishio, Takashi (2022-10-20). “Does coding in Pythonic zen peak performance?: Preliminary experiments of nine Pythonic idioms at scale” . Proceedings of the 30th IEEE/ACM International Conference on Program Comprehension . ICPC '22. New York, NY, USA: Association for Computing Machinery. pp. 575–579. doi :10.1145/3524610.3527879 . ISBN 978-1-4503-9298-3 . https://doi.org/10.1145/3524610.3527879
^ Zhang, Zejun; Xing, Zhenchang; Xia, Xin; Xu, Xiwei; Zhu, Liming (2022-07-12), Making Python Code Idiomatic by Automatic Refactoring Non-Idiomatic Python Code with Pythonic Idioms , arXiv :2207.05613
^ Phan-udom, Purit; Wattanakul, Naruedon; Sakulniwat, Tattiya; Ragkhitwetsagul, Chaiyong; Sunetnanta, Thanwadee; Choetkiertikul, Morakot; Kula, Raula Gaikovina (2020-09-05), Teddy: Automatic Recommendation of Pythonic Idiom Usage For Pull-Based Software Projects , arXiv :2009.03302