Encapsulation, inheritance and polymorphism

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jul 18 14:43:23 CEST 2012

On Tue, 17 Jul 2012 12:01:21 +0100, Lipska the Kat wrote:

> For the past 9 years I have been developing in Java
> Anyway, I'm looking at Python as a rapid prototyping language. I have an
> idea and just want to get it down in basic outline code as quickly as
> possible before it departs my aging brain... 

A couple of good resources for you to read, written by a top Python 
developer who also knows Java backwards:


You will occasionally (i.e. about sixteen times a day) read some Python 
programmer tossing out comments like "Flat is better than nested", often 
in quotes. P.J. Eby does this at least twice in the first link above. 
What the hell are they talking about?

They're talking about the Zen of Python, a list of a dozen or so slightly 
tongue in cheek mottoes meant to encapsulate the (often deliberately 
contradictory) coding philosophy that Python developers should aspire 
too. At the interactive Python prompt, enter "import this" (without the 
quotes) and be enlightened.

Keeping the Zen in mind as an ideal is incredibly useful. Arguing over 
whose opinion is more true to the Zen is a waste of time.

> I'm not used to using
> variables without declaring their type ... (well I used to do Visual
> Basic many years ago) It just seems so weird, 

Compared to Java, or Haskell, or Ada, Python may seem like an incredibly 
sloppy language. A lot of that sloppiness comes from the lack of compile-
time type-checking. And that's probably true: Haskell's type checker can 
detect infinite loops, by Jove! Python won't even warn you that you're 
about to blow away a built-in function. (Don't worry though, you can 
easily get it back again.)

(But at least Python isn't Perl, or PHP.)

While Python doesn't (can't!) check the type of data at compile-time, but 
it does check the type of data at run-time. This ain't assembly language, 
where there's only one type, bytes! I came from a Pascal background, and 
at first I found the lack of type declarations rather concerning. But 
soon I found it liberating.

Especially once I started using Python interactively, at the REPL.

Most arguments about which is better, compile-time type checking or run-
time type checking, miss the point: both have advantages, and 
disadvantages. Which is "better" depends on what you want to do.

Python is deliberately designed to be *fast to write*. It gives up a 
little bit of safety for that. But not as much as you might think. Type 
errors are less frequent than other errors (errors of logic, bad data, 
etc.). And so in the time it might take a Java programmer to satisfy the 
compiler's insistence on type correctness, a Python programmer has 
probably written the program *and* the unittests and given the customer 
the first iteration of the application.

At least, that's the theory.

But you probably already know that. After all, that's why Python is the 
rapid-application-development language, not Java.

> and what's this obsession
> with 'correct' indentation of code ???

Nearly everyone rights correctly indented code anyway. At least, those 
who don't, you don't want on your project. Python just takes it one step 
further, and makes correctly indented code mandatory rather than optional.

The plus side is, no more style wars over where the braces go, no more 
hunting for lost braces. The downside is, if you paste code into a dumb 
application (like many email clients!) that strips whitespace, your code 
will break. So don't do that.



More information about the Python-list mailing list