Zen of Python
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2026/11/16 03:21 UTC 版)
Zen of Pythonとは、プログラミング言語Pythonの設計に影響を与えた、コンピュータプログラムを書くための19の「原則」 を集めたものである[1]。これらの原則に沿ったPythonのコードはしばしば「Pythonic」と呼ばれる[2]。
ソフトウェア工学者のティム・ピーターズがこの原則集を書き、1999年にPythonのメーリングリストに投稿した[3]。ピーターズによる原則集では、Python言語の原作者のグイド・ヴァンロッサムに言及した「グイドが埋める」とされた20番目の原則が空欄のまま残されており、現在も埋められていない。
Zen of Pythonはその後、Python Enhancement Proposal(PEP)の1つであるPEP 20として文書化され、パブリックドメインで公開された[4]。また、Zen of PythonはPythonインタプリタにイースターエッグとして含まれており、import thisと入力することで表示できる[1][4][注釈 1]。
2020年5月、GNU Mailmanの開発者であるバリー・ワルシャワ(Barry Warsaw)が、Zen of Pythonを歌詞とした楽曲を発表した[5][6]。
原則
原則は以下の通りである[注釈 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.
(明白なやり方が1つ、できれば1つだけあるべきだ。)[注釈 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!
(名前空間はとてつもなく素晴らしいアイデアだ。もっと使っていこう!)
Pythonicであること
原則の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の公開以来、その効果と開発者の間での実際の使用に関する調査が行われてきた。技能の異なる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で確認できる。
- ↑ 以下の各原則に付したリンクは、理解を助けるためにウィキペディアで追加したものであり、Zen of Pythonの原文にはない。日本語訳もウィキペディアで付したものである。
- ↑ この原則と最後の原則では、ダッシュの書き方が意図的に不統一にされている。これは、ダッシュの書式に関する様々な流儀を踏まえたものである[7]。
- ↑ インタプリタのイースターエッグではこの原則は「Although never is often better than *right* now.」と表示される。これは、文字の装飾ができないことが多いプレーンテキストでの通信において、強調をアスタリスクで表す長年の慣習に従ったものである。
- ↑ この研究で用いられたPythonicイディオムの一覧はslimshadyiam
.github で公開されている。.io /ZenYourPython /
出典
- 1 2 Reitz, Kenneth (2011年 - 2019年). “Code Style”. The Hitchhiker’s Guide to Python. 2019年3月26日閲覧。
- 1 2 “用語集”. 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日閲覧。
- 1 2 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日閲覧。
- 1 2 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時点におけるアーカイブ。
- ↑ “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
- ↑ 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
- ↑ 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
関連項目
- 設定より規約
- やり方は一つじゃない - Perlのスローガン
外部リンク
- PEP 20 - peps.python.org
- Tim PetersによるPEP 20 ~ The Zen of Python
- Zen of Pythonのページへのリンク