OT probably but still relevant (was Re: Looking for a good introduction to object oriented programming with Python)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Aug 7 05:44:22 EDT 2012


On Mon, 06 Aug 2012 17:23:19 +0100, lipska the kat wrote:

> On 06/08/12 13:19, rusi wrote:

>> I suggest this
>> http://steve-yegge.blogspot.in/2006/03/execution-in-kingdom-of-
nouns.html
> 
> http://bpfurtado.livejournal.com/2006/10/21/

Unfortunately the author (Bruno Furtado) has missed the point. He seems 
to think that Kingdom of Nouns ("the Rant") is an anti-OO screed. It is 
not. There is more to OO than Java, and criticism of Java does not mean 
"objects are bad".


Nor is it that Kingdom of Nouns believes the problem is "Two lines per 
source code file!!!". It isn't just that two extra lines when you declare 
a function. It is the extraneous, unnecessary extra layer each time you 
read (or write, but mostly read) the method, and the ever-more abstract, 
deep layers of Builders, Getters, Setters, Doers and so forth.

In Python, occasionally you will come across code like this:

value = time.time()

which is the time function in the time module. Pretty skanky, right? It's 
not likely that you have a whole bunch of spam.time, foo.time, 
physics.time etc. in your code base, right? If you do, then maybe this is 
justified, but how often does that happen?

Having that duplicated time.time is a code smell that says "you might 
have too many code layers here, better look at this a bit more closely".

Now imagine if this was a more Java-ish language:

value = time.time.time()

where you have method time of class time in module time. And you have to 
call it that way *all the time* (pun not intended).

That's not so much a "might" have too many layers, as "almost certainly".

In Java, all those dot look-ups happen at compile time and are fast. In 
Python, they happen at runtime, and while they're still often fast, 
sometimes they can be slow. But that's not the real problem. The real 
problem is reading and writing it.

Python solves that two ways:

1) Modules, classes, methods and functions are first-class objects, so 
you can do this:

from time import time
# or if you prefer, "import time; time = time.time"
value = time()


2) It doesn't force you to use the same number of layers for 
*everything*. If you have code that will benefit from two, or three, or 
seventeen, layers, you are free to do so:

value = (time.time() + spacetime.Time.time() - Spam.time.flavour()
        + Cube.time())

And if not, then don't:

value = time() + spacetime.time() - flavour() + Cube.time()


It is this which is the point of Kingdom of Nouns. It is not an anti-OO 
screed. It is not an argument against namespaces, or encapsulation, or 
especially not that Java methods take an extra "Two lines per source 
file". It is that Java forces a single mental model on you, all the time, 
whether it makes for better, more readable code or not.



-- 
Steven



More information about the Python-list mailing list