What are modules really for?

Magnus Lycka lycka at carmen.se
Thu Aug 11 08:34:15 EDT 2005


N.Davis wrote:
> Functions existing in a module? Surely if "everything is an object" (OK 
> thats Java-talk but supposedly Python will eventually follow this too) 

int too? ;)

Actaully, I think Python and Java are fairly much on equal footing
here, with Java possibly being slightly behind rather than ahead.

> then there should be nothing in a module thats not part of a class. Even 
> a static method is simply a class function that operates on the 
> "collection of all instances" rather than a single instance.

Object != class instance.

In Python, almost everything is an object. Files, classes, instances,
ints, modules, functions etc can be passes around, assigned to new
names, referenced in containers, printed etc etc. This is very helpful.
Forcing people to write a lot of bloat code around their functions
isn't really that helpful...

I can appreciate a language like Smalltalk, designed from ground up with
a pure OO approach--but it seems to me that Java's design was basically
to make a C++ derivate with bars and safety belts in a lot of places to
stop programmers from shooting themselves in the foot.

> Related classes in the same file? Be careful. Doesn't anything "knowing" 
> about anything else compromise encapsulation? Why would 
> properly-designed classes have such a close relationship?

In your code, nothing ever knows anything about anything else? :)

I'm sure you have written code where a class knows of some other
class, for instance through inheritance or composition. It does
actually even happen that it's reasonable to let two classes be
mutually aware of each other, even if it's not as common as one
way relationships. It's often better to have a third class control
the relationship between the two, but that's my design decision,
not Guido van Rossum's or James Gosling's.

There is a fundamental difference in the philosophies behind the
design of Java and Python. It seems to me that Java is designed
to make is difficult for programmers to write bad code, while
Python is designed to make it easy to write good code. This makes
a big difference.

Another aspect to consider is that Python classes are typically
shorter than Java classes, since Python isn't as noisy as Java.

Perhaps you should have a look at HTMLGen for instance. It's a
library for generating HTML code. (Pretty old.) IIRC it has a lot
of classes that are just one line long. These classes represent
simple tags, and all behaviour is defined in some base class, so
the only thing that differs between i.e. the I class and the B
class is that str(I('Hello')) should be rendered as <I>Hello</I>
and str(B('Hello')) should be rendered as <B>Hello</B>. I don't
know what the name of their base class is, but assuming 'X', they
are just implemented as 'class I(X):pass' and class B(X):pass'.
It would have been a bit silly if Python had forced them out in
different files...




More information about the Python-list mailing list