Python is readable

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 15 23:55:43 EDT 2012


On Fri, 16 Mar 2012 00:32:52 +0100, Kiuhnm wrote:

> Pick up two math books about the same topic but on two different levels
> (e.g. under-graduated and graduated). If you compare the proofs you'll
> see that those in the higher-level book are almost always sketched. Why
> is that? Because they're more readable for a mature reader.

No. They're not more readable. They simply have less to read (per proof), 
or another way of putting it, you can fit more of the good stuff that the 
experienced reader wants ("yes yes, I know about the associative law, get 
to the part where you prove the Goldbach conjecture...") in the same 
amount of space. The text is compressed by leaving out the parts that an 
experienced reader can infer from her knowledge of the domain.

Since an expert can infer meaning more quickly than they can read the 
actual words, this is a big win for efficiency. But the text is not more 
readable, there's just less to read for a given result. The same amount, 
or even more, brain processing occurs. It just happens in a different 
part of the brain.

In Python terms, it's analogous to moving something out of a pure-Python
O(log n) binary tree into a fast O(n) linear array written in C. Until 
the concepts get complex enough, it is faster for the expert to infer 
meaning than to read explicitly, even though technically more work is 
probably being done.


Experts often find pedagogical texts harder to read because it seems 
dumbed down. It's as if they are reading text like this:

    The cat, which is a small animal with fur known for being 
    aloof and yet attractive to many people, sat on the mat, 
    which is like a rug or blanket except thicker, while Jack, 
    a small male child, and Jill, a young female child, ran, 
    that is to say travelled quickly by lifting their feet and 
    moving forward in such a fashion that sometimes both feet 
    are in the air simultaneously, up the hill, a moderately-
    sized elevation of land smaller than a mountain.


It gets painful after a while because, as an expert, you can interpret 
the subtext quicker than you can read the actual words. But when teaching 
non-experts, you can't expect the reader to interpret the subtext quickly 
or at all. Which is more readable, that is to say, more comprehensible?

    The wix sat on the zaj, while Megf and Parz vev up the leff.

Lacking any domain knowledge, the above carries virtually no information 
at all. We know that wixes can sit on things, and that's about it.

Contrast this:

    The wix, which is a small animal with fur known for being 
    aloof and yet attractive to many people, sat on the zaj, 
    which is like a rug or blanket except thicker, while Megf, 
    a small male child, and Parz, a young female child, vev, 
    that is to say travelled quickly by lifting their feet and 
    moving forward in such a fashion that sometimes both feet 
    are in the air simultaneously, up the leff, a moderately-
    sized elevation of land smaller than a mountain.

Despite having about 8 times as much content, and being one long run-on 
sentence, this version is far more comprehensible. (In practice, I 
wouldn't define terms in the sentence I was using them in this fashion. 
Or at least not more than one per sentence. Very long sentences have 
their own readability costs.)

When people talk about readability, they normally mean to ask how much 
mental effort is needed to interpret the meaning of the text, not how 
much time does it take to pass your eyes over the characters. In other 
words they are actually talking about comprehensibility.

Well obviously that depends on who is doing the reading. To somebody in 
the know, meaning can be incredibly compressed. You can crack up Monty 
Python fans with just two words: "Norwegian Blue". To those not in the 
know, that's incomprehensible.

When speaking about programming languages, the reader who is judging 
readability is assumed to be:

- somebody with a good grasp of natural language, normally 
  English, and capable of understanding sentences containing
  loops and conditionals such as

    "soak and rinse the beans three times"
    "scrub the potatoes until the dirt is gone"
    "if the tap is dripping, replace the washer"

- possessing an intuitive understanding of such concepts as 
  "is-a" and "has-a" (Fido is a dog, Fido has a tail);

- possessing a simple knowledge of basic arithmetic and logic;

- able to intuit the meaning of programming concepts if they
  match simple natural language concepts (e.g. print, delete, 
  but not necessarily import, delegate, and certainly not 
  currying, closures, monads);

- but not familiar with the programming language, its data 
  model, or the details of its functions and commands;

- and of average intelligence, neither a dummy nor a genius. 

(Despite the proliferation of books with titles like "Programming For 
Utter Morans", they aren't actually written for dummies.)

To judge the readability of a language, we have to put ourselves into the 
position of such a beginner: ignorant but not stupid. To judge the 
expressibility and power of a language, we have to put ourselves into the 
position of an expert who knows all the idioms. The trick is to increase 
power without hurting readability.

For example, in the late 80s or early 90s, Apple introduced the 
forerunner to Applescript, Hypertalk. Hypertalk is *extremely* readable:

put the result into x
add 15 to x
go to the second card of background "Data"
put x after word 7 of field "Age"
get field "Name"
put it into field "Alias"

Aside: there's a non-GUI version of Hypertalk-on-steroids available from 
here: http://code.google.com/p/openxion/

Very readable indeed. You can possibly even infer part of the data model 
from the code (everything is text; text is stored in fields, which exist 
on cards collected into groupings called backgrounds).

But, frankly, it's not too expressible. It's too verbose for experts. The 
data model is too simple. (Modern versions like OpenXion have a richer 
data model not as strongly tied to the original Hypercard GUI.)

I think Python is pretty close to the top of the readability versus 
expressiveness curve.

That's not to say that Python is the optimal combination, or can't be 
improved, or even that every language should compromise expressiveness 
for comprehensibility (or vice versa). 



-- 
Steven



More information about the Python-list mailing list