Zen of Python
{{Short description|Programming language design principles}}
The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language.{{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
|website=The Hitchhiker’s Guide to Python
|date=2011–2019
|access-date=March 26, 2019}} Python code that aligns with these principles is often referred to as "Pythonic".
Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing list in 1999.{{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}} 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 Proposals and was released into the public domain.{{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}} It is also included as an Easter egg in the Python interpreter, where it can be displayed by entering
In May 2020, Barry Warsaw (developer of GNU Mailman) used it as the lyrics to a song.{{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}}{{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}}
Principles
File:The_Zen_of_Python_illustrated.png
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}}
{{Plainlist|
- 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.{{efn|The formatting of the dashes in this line and the final is purposely inconsistent, in reference to the varying formatting conventions.{{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=}}}}
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
- Although never is often better than {{em|right}} now.{{efn|In the interpreter easter egg, this is written as "Although never is often better than *right* now." This follows a longstanding convention of plain-text communication — in which common formatting features are often impossible — where emphasis is represented with asterisks.}}
- 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!
}}
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. The official definition of "Pythonic" is:{{Cite web |title=Glossary |url=https://docs.python.org/3/glossary.html#term-Pythonic |access-date=2024-02-07 |website=Python Documentation |language=en}}
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 afor
statement. Many other languages don’t have this type of construct, so people unfamiliar with Python sometimes use a numerical counter instead:
for i in range(len(food)):
print(food[i])
As opposed to the cleaner, Pythonic method:
for piece in food:
print(piece)
Code that is difficult to understand or reads like a rough transcription from another programming language is called unpythonic.{{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}}
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".{{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://doi.org/10.5167/uzh-156901 |archive-date=2018-10-03 }} Researchers extended this case study to explore the use of Python 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.{{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}} Writing Python code that aligns with the Zen of Python may save memory and run time of Python programs.{{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}} The desire to write in Pythonic code has led to refactoring tools to help programmers achieve this goal.{{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}}{{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}}
See also
Notes
{{notelist}}
References
{{reflist}}
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:Essays about computing