|Jeremy Hylton : weblog : 2003-10-13|
Monday, October 13, 2003, 1:50 p.m.
The Python standard library steers an even course between heavy object-oriented style and a lower level style that eschews objects. Bjarne Stroustrup advocates a similar style for C++ programs.
artima.com is running the first part of an interview with Stroustrup. The low-level style in C++ is much different than the low-level style in Python (and the OO-style is more involved, too).
Stroustrup defends structs, which are a perfectly good way to represent simple data. He advocates using a class once there are internal invariants that need to be maintained to use an object. In Python, you can use tuples or classes as a structs. I tend to prefer classes for data structures, because you can refer to the parts by name.
Going to the big class hierarchy is again, you write more code than you need to, and you get too much connection between different parts. I particularly dislike classes with a lot of get and set functions. That is often an indication that it shouldn't have been a class in the first place. It's just a data structure. And if it really is a data structure, make it a data structure.
In addition to the connections between classes, inheritance forces you to think about the contract between a base class and its subclasses. Joshua Block writes about this problem in Effective Java -- in particular the rule "Design for inheritance or else prohibit it." (It's a good book even if you don't program in Java.)
The C++ standard library provides some simple data structures like strings and vectors that improve on C++. Python's built-in datatypes are in the same boat.
The Python standard library varies widely in style. There are very old modules like nntplib that were written when Guido was style learning Python style. There are newer frameworks like logging that tend towards OO style, or threading that is modeled explicitly on Java's threads.