Zen of Python: Difference between revisions

Content deleted Content added
Restored revision 1213693014 by Erictleung (talk): Fixing vandalism
Revert Python code changes and add a footnote on its verbatim quote from the Glossay page
 
(13 intermediate revisions by 9 users not shown)
Line 26:
|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]]) wroteused it as the lyrics to musica 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==
Line 50:
* 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|plain-text]] communication — in which common formatting features are often impossible — where [[Emphasis (typography)|emphasis]] is represented with asterisks.}}
* If the [[Implementation|implementation]] is hard to [[Rubber_duck_debugging|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&nbsp;– let's do more of those!
Line 57:
 
== 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)):
Line 68 ⟶ 70:
</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://towardsdatascience.com/how-to-be-pythonic-and-why-you-should-care-188d63a5037e|title=How to be Pythonic and why you should care|first=Robert|last=Clark|date=26 April 2019|website=Medium|access-date=20 January 2021|archive-date=13 August 2021|archive-url=https://web.archive.org/web/20210813194313/https://towardsdatascience.com/how-to-be-pythonic-and-why-you-should-care-188d63a5037e?gi=dd6bc15118b3|url-status=live}}</ref><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://doiweb.archive.org/10.5167web/20240324015819/https://www.zora.uzh-.ch/id/eprint/156901/1/paper.pdf |archive-date=2018-102024-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 |url=http://arxiv.org/abs/2207.05613 |access-date=2024-02-07 |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 |url=http://arxiv.org/abs/2009.03302 |access-date=2024-02-07 |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 ==
Line 91 ⟶ 93:
[[Category:Programming principles]]
[[Category:Python (programming language)]]
{{Essay-stub}}