Content deleted Content added
Erictleung (talk | contribs) →Being Pythonic: described -> referenced |
Erictleung (talk | contribs) Revert Python code changes and add a footnote on its verbatim quote from the Glossay page |
||
(30 intermediate revisions by 13 users not shown) | |||
Line 7:
|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
Line 17:
|url=https://wefearchange.org/2010/06/import-this-and-zen-of-python.html
|title=We Fear Change
|website=wefearchange.org}}</ref> --> Peters'
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
Line 24:
|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]])
==Principles==
Line 38:
* [[KISS_principle|Simple]] is better than complex.
* Complex is better than complicated.
* Flat is better than [[Nesting_(computing)|nested]].
* [[Minimalism#Design,_architecture,_and_spaces|Sparse]] is better than dense.
* [[Computer_programming#Readability_of_source_code|Readability]] counts.
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 – 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://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
== See also ==
Line 90 ⟶ 93:
[[Category:Programming principles]]
[[Category:Python (programming language)]]
|