The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Steven D'Aprano steve at pearwood.info
Mon Mar 21 21:01:37 EDT 2016


On Tue, 22 Mar 2016 06:43 am, BartC wrote:

> This code was adapted from a program that used:
> 
>     readstrfile(filename)
> 
> which either returned the contents of the file as a string, or 0.

What an interesting function. And I don't mean that in a good way.

So if it returns 0, how do you know what the problem is? Mistyped file name?
Permission denied? File doesn't actually exist? Disk corruption and you
can't open the file? Some weird OS problem where you can't *close* the
file? (That can actually happen, although it's never happened to me.) How
do you debug any problems, given only "0" as a result?

What happens if you read (let's say) a 20GB Blue-Ray disk image?

Such a function is good only for the quickest and dirtiest of quick and
dirty scripts. But if you're going to be quick and dirty, in Python you
might as well be even quicker and dirtier:

open(filename).read()


and just don't bother to do any error handling at all. If there's a problem,
you'll get a traceback and the program will halt.


> That's all. My Python version was thrown together as I don't know if
> there's a similar function to do the same.
> 
> If you want to talk about Pythonic, I don't see why that file API
> doesn't count (the original is buried in a library).

What library? Who wrote it?

 
> Or does Pythonic mean bristling with exceptions and classes and what-not?

Not everything written in Python is Pythonic. In fact, there's really no
objective definition of "Pythonic". It's like art, "I'll know it when I see
it". It means elegant use of Python idioms, but people can and do argue
over what's elegant and idiomatic.

This is no different from any other language. How do you know the difference
between good, idiomatic C code, and poor C code written by people who don't
know the language?


You can start to get an idea of Pythonic by reading the Zen of Python:

import this


but beware that, like most koans, the Zen is deliberately contradictory, and
contains at least one subtle joke.

"Pythonic" code should not be "too clever", just clever enough. If it is
hard to understand, it's probably not Pythonic. If it slavishly follows
recipes and practices from other languages, it's probably not Pythonic.

Code which does a lot of clever bit-twiddling to optimise arithmetic (as
seen often in C code) is probably not Pythonic, because in Python the
performance difference between `x << 1` and `x*2` is probably
insignificant. So write what is most clear, not what you write in C code.

Code which has dozens of classes like Java is usually not Pythonic. See:

http://dirtsimple.org/2004/12/python-is-not-java.html

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html


If you're writing code to solve problems that don't occur in Python, then
your code is not Pythonic.

Pythonic code probably uses a lot of iterables:

for value in something:
    ...


in preference to Pascal code written in Python:

for index in range(len(something)):
    value = something[index]
    ...


or worse:

index = 0
while index < len(something):
    value = something[index]
    ...
    index += 1



(I don't know where that while-loop idiom comes from. C? Assembly? Penitent
monks living in hair shirts in the desert and flogging themselves with
chains every single night to mortify the accursed flesh? But I'm seeing it
a lot in code written by beginners. I presume somebody, or some book, is
teaching it to them. "Learn Python The Hard Way" perhaps?)


Pythonic code tends to reduce boilercode, not increase it. One line for
the "for value" loop; two lines for the Pascal version; four lines for the
hairshirt version using while.

But on the other hand, Pythonic code doesn't try too hard to minimise the
number of lines of code. If you find yourself asking "how do I write this
as one line", and the answer is some awful tricky code, then it probably
isn't Pythonic. If it takes two lines, it takes two lines, and not
everything needs to be a one-liner.

Perl one-liners are not Pythonic.


Really, the only way to learn what is and isn't Pythonic is to read and
write so much Python code that you learn what's elegant, idiomatic,
Pythonic code.

Unfortunately, real world code, which has to deal with errors and exceptions
and optimizations, is rarely Pythonic. But if you start with something like
the Python standard library, close one eye and squint, you can usually see
the Pythonic code peeking out from behind the error checking and
optimizations.


-- 
Steven




More information about the Python-list mailing list