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 Answers4

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?

S.Lott
  • 45,522
  • 6
  • 93
  • 155
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.

Demian Brecht
  • 17,585
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