Python – Formal Definition of ‘Pythonic’ Beyond PEP8

python

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?

Best Answer

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?

Related Topic