Content deleted Content added
Erictleung (talk | contribs) Revert Python code changes and add a footnote on its verbatim quote from the Glossay page |
|||
(116 intermediate revisions by 66 users not shown) | |||
Line 1:
{{Short description|Programming language design principles}}
[[File:Zen Of Python.png|300px|thumb|The Zen of Python output in a terminal]]
The '''Zen of Python''' is a collection of 19 [[Coding conventions|"guiding principles"]] for writing [[computer program]]s that influence the design of the [[Python (programming language)|Python]] [[programming language]].<ref name="hitchhiker">{{cite web
|url=http://docs.python-guide.org/en/latest/writing/style/#zen-of-python
|title=Code Style |id=§ Zen of Python
|first=Kenneth |last=Reitz <!-- |author-link=Kenneth Reitz -->
|website=The Hitchhiker’s Guide to Python
|date=2011–2019
|access-date=March 26, 2019}}</ref> Python code that aligns with these principles is often referred to as "Pythonic".<ref name=":1" />
[[Software engineer]] [[Tim Peters (software engineer)|Tim Peters]] wrote this set of principles and posted it on the Python [[mailing list]] in 1999.<ref name="Peters 1999-06-04">{{cite web
|url=https://mail.python.org/pipermail/python-list/1999-June/001951.html
|title=The Python Way
|publisher=[[Python Software Foundation]] |date= June 4, 1999
|first=Tim |last=Peters |author-link = Tim Peters (software engineer)
|access-date=March 26, 2019}}</ref><!-- <ref>{{cite web
|url=https://wefearchange.org/2010/06/import-this-and-zen-of-python.html
|title=We Fear Change
|website=wefearchange.org}}</ref> --> Peters' list left open a 20th principle "for Guido to fill in", referring to [[Guido van Rossum]], the original author of the Python language. The vacancy for a 20th principle has not been filled.
Peters' Zen of Python was included as entry number 20 in the language's official [[Python Enhancement Proposal]]s and was released into the [[public ___domain]].<ref name="pep20">{{cite web
|url=https://www.python.org/dev/peps/pep-0020/
|title=PEP 20—The Zen of Python
|publisher=[[Python Software Foundation]] |date= August 19, 2004
|first=Tim |last=Peters
|access-date=March 26, 2019}}</ref> It is also included as an [[Easter egg (software)|Easter egg]] in the Python [[Interpreter (computing)|interpreter]], where it can be displayed by entering <syntaxhighlight lang="python" inline>import this</syntaxhighlight>.<ref name="hitchhiker"/><ref name="pep20" />{{Efn|The source code for the Zen of Python can be found [https://github.com/python/cpython/blob/main/Lib/this.py on Github].}}
In May 2020, Barry Warsaw (developer of [[GNU Mailman]]) used it as the lyrics to a song.<ref>{{Cite web |last=Warsaw |first=Barry |date=10 May 2020 |title=The Zen of Python |url=https://wefearchange.org/2020/05/zenofpython.rst.html |url-status=live |archive-url=https://web.archive.org/web/20200603010307/https://wefearchange.org/2020/05/zenofpython.rst.html |archive-date=2020-06-03 |website=We Fear Change}}</ref><ref>{{cite AV media |url=https://www.youtube.com/watch?v=i6G6dmVJy74 |title=The Zen of Python |date=23 May 2020 |last=Warsaw |first=Barry |publisher=The Zbwedicon |archive-url=https://ghostarchive.org/varchive/youtube/20211211/i6G6dmVJy74 |archive-date=2021-12-11 |url-status=live |website=[[YouTube]]}}{{cbignore}} {{webarchive |url=https://web.archive.org/web/20200603010403/https://www.youtube.com/watch?v=i6G6dmVJy74 |date=2020-06-03 |nolink=y}}{{cbignore}}</ref>
==Principles==
[[File:The_Zen_of_Python_illustrated.png|thumb|The Zen of Python illustrated]]
The principles are listed as follows:{{efn|Wikipedia links below are added for further reference and understanding, and were not explicitly linked in the Zen of Python}}
<blockquote
{{Plainlist|
Beautiful is better than ugly.▼
* [[Minimalism#Design,_architecture,_and_spaces|Sparse]] is better than dense.
* [[Computer_programming#Readability_of_source_code|Readability]] counts.
* [[Edge_case|Special cases]] aren't special enough to break the rules.
* Although [[Perfect_is_the_enemy_of_good|practicality]] beats purity.
* Errors should never pass silently.
* Unless explicitly silenced.
* In the face of [[Ambiguity_aversion|ambiguity]], refuse the temptation to guess.
* There should be one-- and preferably only one --obvious way to do it.{{efn|The formatting of the [[Dash|dashes]] in this line and the final is purposely inconsistent, in reference to the varying formatting conventions.<ref>{{Cite web|last=|first=|date=|title=Issue 3364: An ortographical typo in Zen of Python text - Python tracker|url=https://bugs.python.org/issue3364|archive-url=|archive-date=|access-date=2021-02-10|website=}}</ref>}}
* Although that way may not be obvious at first unless you're Dutch.
* Although never is often better than
If the implementation is hard to explain, it's a bad idea.▼
* If the [[Implementation|implementation]] is
* [[Namespaces]] are one honking great
</poem></blockquote>▼
}}
== Being Pythonic ==
One of the principles, "There should be one-- and preferably only one --obvious way to do it", can be referenced as the "Pythonic" way.<ref name=":0" /> The official definition of "Pythonic" is:<ref name=":1">{{Cite web |title=Glossary |url=https://docs.python.org/3/glossary.html#term-Pythonic |access-date=2024-02-07 |website=Python Documentation |language=en}}</ref>{{efn|This definition below is directly quoted to preserve its definition. So as is, it is not self-contained valid Python to run without errors.}}
<blockquote>An idea or piece of code which closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages. For example, a common idiom in Python is to loop over all elements of an iterable using a <code>for</code> statement. Many other languages don’t have this type of construct, so people unfamiliar with Python sometimes use a numerical counter instead:
<syntaxhighlight lang="python">
for i in range(len(food)):
print(food[i])
</syntaxhighlight>
As opposed to the cleaner, Pythonic method:
<syntaxhighlight lang="python">
for piece in food:
print(piece)
</syntaxhighlight>
</blockquote>
Code that is difficult to understand or reads like a rough transcription from another programming language is called ''unpythonic''.<ref>{{Cite web|url=https://docs.python-guide.org/writing/style|title=Code Style – The Hitchhiker's Guide to Python|website=docs.python-guide.org|access-date=20 January 2021|archive-date=27 January 2021|archive-url=https://web.archive.org/web/20210127154341/https://docs.python-guide.org/writing/style/|url-status=live}}</ref>
== In practice ==
Since the release of the Zen of Python, there has been research done on its effectiveness and actual use among developers. Despite the difference in interpretation between beginners and experienced Python programmers, interviews among 13 Python programmers of varying skill show that the Zen of Python "positively influences the way developers write and talk about code".<ref name=":0">{{Cite book |last1=Alexandru |first1=Carol V. |last2=Merchante |first2=José J. |last3=Panichella |first3=Sebastiano |last4=Proksch |first4=Sebastian |last5=Gall |first5=Harald C. |last6=Robles |first6=Gregorio |chapter=On the usage of pythonic idioms |date=2018-10-24 |title=Proceedings of the 2018 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software |chapter-url=https://doi.org/10.1145/3276954.3276960 |series=Onward! 2018 |___location=New York, NY, USA |publisher=Association for Computing Machinery |pages=1–11 |doi=10.1145/3276954.3276960 |isbn=978-1-4503-6031-9 |s2cid=53057358 |oclc=1362712424 |url=https://www.zora.uzh.ch/id/eprint/156901/1/paper.pdf |chapter-url-access=registration |archive-url=https://web.archive.org/web/20240324015819/https://www.zora.uzh.ch/id/eprint/156901/1/paper.pdf |archive-date=2024-03-24 |access-date=2024-02-19 |url-status=bot: unknown }}</ref> Researchers extended this [[case study]] to explore the use of Python [[Programming idiom|idioms]] on [[GitHub]] repositories, and found that the usage of "Pythonic idioms"{{Efn|A list of their Pythonic idioms can be found here https://slimshadyiam.github.io/ZenYourPython/}} increased over time.<ref>{{Cite book |last1=Farooq |first1=Aamir |last2=Zaytsev |first2=Vadim |chapter=There is more than one way to zen your Python |date=2021-11-22 |title=Proceedings of the 14th ACM SIGPLAN International Conference on Software Language Engineering |chapter-url=https://dl.acm.org/doi/10.1145/3486608.3486909 |series=SLE 2021 |___location=New York, NY, USA |publisher=Association for Computing Machinery |pages=68–82 |doi=10.1145/3486608.3486909 |isbn=978-1-4503-9111-5}}</ref> Writing Python code that aligns with the Zen of Python may save [[Computer memory|memory]] and [[Execution (computing)|run time]] of Python programs.<ref>{{Cite book |last1=Leelaprute |first1=Pattara |last2=Chinthanet |first2=Bodin |last3=Wattanakriengkrai |first3=Supatsara |last4=Kula |first4=Raula Gaikovina |last5=Jaisri |first5=Pongchai |last6=Ishio |first6=Takashi |chapter=Does coding in Pythonic zen peak performance?: Preliminary experiments of nine Pythonic idioms at scale |date=2022-10-20 |title=Proceedings of the 30th IEEE/ACM International Conference on Program Comprehension |chapter-url=https://doi.org/10.1145/3524610.3527879 |series=ICPC '22 |___location=New York, NY, USA |publisher=Association for Computing Machinery |pages=575–579 |doi=10.1145/3524610.3527879 |isbn=978-1-4503-9298-3}}</ref> The desire to write in Pythonic code has led to [[Code refactoring|refactoring]] tools to help programmers achieve this goal.<ref>{{Citation |last1=Zhang |first1=Zejun |title=Making Python Code Idiomatic by Automatic Refactoring Non-Idiomatic Python Code with Pythonic Idioms |date=2022-07-12 |arxiv=2207.05613 |last2=Xing |first2=Zhenchang |last3=Xia |first3=Xin |last4=Xu |first4=Xiwei |last5=Zhu |first5=Liming}}</ref><ref>{{Citation |last1=Phan-udom |first1=Purit |title=Teddy: Automatic Recommendation of Pythonic Idiom Usage For Pull-Based Software Projects |date=2020-09-05 |arxiv=2009.03302 |last2=Wattanakul |first2=Naruedon |last3=Sakulniwat |first3=Tattiya |last4=Ragkhitwetsagul |first4=Chaiyong |last5=Sunetnanta |first5=Thanwadee |last6=Choetkiertikul |first6=Morakot |last7=Kula |first7=Raula Gaikovina}}</ref>
== See also ==
* [[Convention over configuration]]
* [[There's more than one way to do it]]
== Notes ==
{{notelist}}
== References ==
Line 33 ⟶ 87:
== External links ==
* [https://www.python.org/dev/peps/pep-0020/ PEP20 on Python website]
* [https://pep20.org/ PEP 20 ~ The Zen of Python by Tim Peters]
[[Category:2004 essays]]
[[Category:Essays about computing]]
[[Category:Programming principles]]
[[Category:Python (programming language)]]
|