6

For me as python programmer having a formal definition for the term "pythonic" would be much more important and less subjective compared to asking more rhetoric (non-formal) questions. This may lead to better communication by argumentation being less subjective (as discussed in here).

PEP8 by Guido is assumed as a starting point despite of some religious dogmatism (e.g. PEP20 is more axiomatic, shorter and simpler, but has no code). Having identified minimalism, readability (human comprehension) and unambiguity (explicit code) is actually the same as paraphrasing PEP20. But is it possible to rewrite zen of python in python? Obviously print('Beautiful is better than ugly') does not capture python in python ;-) Any thoughts on this?

Ultimately I would like to get closer to more logical or mathematical interpretation of the matter. Argument in form of the code answering why family of snippets A is pythonic compared to family of snippets B is also acceptable.

EDIT If possible try to reduce your answer to the following outline. Let us consider all possible syntactically correct program texts generated by some procedure (e.g.). Please note that we a considering only programs written in python (which is a formal language with respective grammar). Also being syntactically correct does not mean semantical correctness (programs may be buggy or without any obvious purpose). Further we observe (assume) that ultimate family POmega of all syntactically correct programs in python must contain a subset of programs which are called Pythonic. Now in order to argue about what does it mean to be pythonic we will use a formal method - another language called Reason which would be less ambiguous (consistent, complete) than natural language. We will define a mapping from programs in Omega into Reason. Such mapping we will call interpretation. We will also produce more mappings in similar manner (e.g. as in) for all individual symbols in Python and their combinations (statements). Now if we would argue about images of such mappings, we would do so in form of proofs in Reason language. The question would be if subset Pythonic indeed exists can we follow that up from aximos in Reason built/reformulated from "Zen of python" or other motivating set of concepts? In fact, the whole deductive core of the language Reason would be built from "Zen" or PEP8. We will have to proof that each program from Pythonic will consist only from statements for images of which in Reason can be shown to satisfy initial axioms of the model language. Different outcomes are plausible:

  1. Subset of Pythonic programs could be empty. We have not found any programs that would satisfy our axioms in Reason.
  2. It could happen that this set of axioms would turn out to be ambiguous (inconsistent or incomplete). Then our original assumption about existence of Pythonic programs would be wrong.
  3. Alternatively axiomatic "Zen" could be simple enough and so well-chosen that Pythonic set is non-empty. Now the question is will such (consistent, complete) set be of practical value, i.e. can we say that such set of axioms is indeed a formal definition of "pythonic"?

This is a draft of how one can try to approach answering the question in formal manner. I would appreciate recommendations on a more elegant approach for solution to the problem (everything except ignoring it). But at the end it matters to me to see how such set of axioms looks like. Hence it will allow in precise (quantitative) way to ask and try to explain questions like why programs have "pythonic" qualities once written according to following python "dogmas"?

BTW One can built a more sophisticated model of Reason by introducing an equivalent of "human factor" - a software developer of the programs. Then the question would be - can human formally claim the program he rights belongs to "Pythonic" subset?

4
  • I appreciate given answers a lot. Looking forward to more. Jan 10, 2012 at 1:38
  • Word of God(vR) is "A foolish consistency is the hobgoblin of little minds". I think you are demanding too much formality and consistency for a language that was precisely not designed this way.
    – Evpok
    Nov 24, 2015 at 9:00
  • yes python is not meant to be more formal than just a working parsing grammar as many more other things in this life.. this has not arose so much interest - apparently nobody cares :) Nov 24, 2015 at 10:29
  • There cannot be an algorithmic test for "pythonicity" just by looking at a program's source code. This is just because whether a program is Pythonic doesn't just depend on the source code, it depends on what the source code is supposed to be doing. The same source, character per character, could be considered Pythonic or not depending on its intended semantics. E.g. usersName = argv[0]; print usersName is good for a program designed to print the user's name, bad for a program intended to print the user's favorite color.
    – Jack M
    Jul 10, 2018 at 8:10

4 Answers 4

11

A formal definition for the term "pythonic" would be largely impossible.

It's as impossible as a definition for "aesthetic" or "convenient" or "delightful".

It can't be less subjective than it is now.

Why not?

Because Python is merely as aesthetic judgement based on the opinions of a fairly large number of people. The GvR points are respected, but do not have the force of law. Many people write code for the Python world, and their various inputs influence other developers. It's a vast social network of people all liking (and disliking) the code they read.

Ultimately I would like to get closer to more logical or mathematical interpretation of the matter

You would do well to start with studying art, aesthetics and philosophy first. Then, after mastering aesthetics and morals, switch to ontology so that you can encode what you learned about truth and beauty. Once you've finished that, you'll have a formal definition of Pythonic along with formal definitions of all the various subjective values that we hold.

axioms ... built/reformulated from "Zen of python"

It's a poem. It's value is entirely aesthetic. What more evidence does one need that Pythonic is a value judgement based on depth of experience?

0
5

Good code is good code whether one person deems it "Pythonic" or not. If it scales well, is tested, documented, easily read (doesn't use strange language hacks, etc) and follows sound architectural principles (DRY, SOLID, etc) then at the end of the day, who cares?

As "zen" is completely subjective to the reader, there is no measurable way of defining "Pythonic" code without some religious debate sparking. Best practices and standardization (such as PEP8) are a good start to achieving quality through automated consistency, but by no means gives a full definition to "Pythonic" code.

3
  • Thnx for replying. Following (DRY, SOLID, etc) is the best one can do, correct? Does further analyses makes sense (too complicated, almost impossible)? First paragraph gives a modern industrial concept of successful software, which is.. well (forgive me) industric, but not immediately spelled in python. The question is why it works? According to my observation as soon as one gets more formal (in terms of logic) the limit of following DRY+SOLID => inconsistency. Second paragraph may be supported by common sense, experience, way of coming to justify code. Point is to avoid such conversations :-) Jan 10, 2012 at 0:04
  • @YauhenYakimovich: IMHO, while following architecture principles is not the best that one can do, it's the most practical. In my experience, the more rules instilled the less they will be followed. With the number of debates raging on what the definition of "Pythonic" is (I just throw up my hands and walk away from most now), it's proven to be a topic as hot as "which editor is better: Vi or Emacs?". Jan 10, 2012 at 0:18
  • cannot agree more. Architecture does it. What part of it is depended on python? That is why I wonder if making things formal will help. It is not really a question of whether or not it is possible to give formal definition. It is more of a problem making it to be "accessible" for a software developer to a certain extend. Python turned out to be an appealing and very much successful. I believe that initially following dogmatic path was very useful, but right now it slows me down. To develop further I may need to study the "phenomena" of python. That is why "pythonic". Jan 10, 2012 at 1:17
0

To produce a test for '"pythonicity"' as pointed out in this Guido quote is obviously a non-trivial enterprise, but the test itself is very much nice to have. Now the current procedure is well known:

  1. Use automated quality control tools as pep8.py or pylint (as pointed by Demian)
  2. Perform codereview, where code is tested for pythonicity by human-based revision.
  3. Finally you can ask community or Guido for the opinion, if code review is not resolved.

All of this can be semi-automated to a certain extend. In fact, communities like Q&A Stack Exchange projects are facilitating to test code for "pythonicity" right now.

Is there a better way to formalize such a test? As difficult as it seems a non-supervised test for pythonicity would be able to formalize "beauty" of the code making human interpretation superfluous. Most importantly it will fully include the human judgment. Topics like this are becoming less philosophical and much more practical if they are studied in theoretical AI or similar computer science.

Now I would not recommend you to answer this question any further unless you think you have a solution.

-4
funcs = dir(__builtins__)
help funcs[1]

Is my favorite

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.