MindView Inc.
[ Book Home Page ] [ Free Newsletter ]
[ Seminars ] [ Seminars on CD ROM ] [ Consulting ]

Thinking in Python
Revision 0.1.2 (12/31/01) -- Incomplete and Unfinished

by Bruce Eckel ©2002 MindView, Inc.

[ Previous Chapter ] [ Table of Contents ] [ Index ] [ Next Chapter ]

Introduction

This is a book about design that I have been working on for years, basically ever since I first started trying to read Design Patterns (Gamma, Helm, Johnson & Vlissides, Addison-Wesley, 1995), commonly referred to as the Gang of Four[1] or just GoF). Add Comment

There is a chapter on design patterns in the first edition of Thinking in C++, which has evolved in Volume 2 of the second edition of Thinking in C++, and you’ll also find a chapter on patterns in the first edition of Thinking in Java. I took that chapter out of the second edition of Thinking in Java because that book was getting too big, and also because I had decided to write Thinking in Patterns. That book, still to be finished, has become this one. The ease of expressing these more complex ideas in Python will, I think, finally allow me to get it all out. Add Comment

This is not an introductory book. I am assuming that you have worked your way through at least Learning Python (by Mark Lutz & David Ascher; OReilly, 1999) or an equivalent text before coming to this book. Add Comment

In addition, I assume you have more than just a grasp of the syntax of Python. You should have a good understanding of objects and what they’re about, including polymorphism. Add Comment

On the other hand, by going through this book you’re going to learn a lot about object-oriented programming by seeing objects used in many different situations. If your knowledge of objects is rudimentary, it will get much stronger in the process of understanding the designs in this book. Add Comment

The Y2K syndrome

In a book that has “problem-solving techniques” in its subtitle, it’s worth mentioning one of the biggest pitfalls in programming: premature optimization. Every time I bring this concept forward, virtually everyone agrees to it. Also, everyone seems to reserve in their own mind a special case “except for this thing that I happen to know is a particular problem.” Add Comment

The reason I call this the Y2K syndrome has to do with that special knowledge. Computers are a mystery to most people, so when someone announced that those silly computer programmers had forgotten to put in enough digits to hold dates past the year 1999, then suddenly everyone became a computer expert – “these things aren’t so difficult after all, if I can see such an obvious problem.” For example, my background was originally in computer engineering, and I started out by programming embedded systems. As a result, I know that many embedded systems have no idea what the date or time is, and even if they do that data often isn’t used in any important calculations. And yet I was told in no uncertain terms that all the embedded systems were going to crash on January 1, 2000[2]. As far as I can tell the only memory that was lost on that particular date was that of the people who were predicting doom – it’s as if they had never said any of that stuff. Add Comment

The point is that it’s very easy to fall into a habit of thinking that the particular algorithm or piece of code that you happen to partly or thoroughly understand is naturally going to be the bottleneck in your system, simply because you can imagine what’s going on in that piece of code and so you think that it must somehow be much less efficient than all the other pieces of code that you don’t know about. But unless you’ve run actual tests, typically with a profiler, you can’t really know what’s going on. And even if you are right, that a piece of code is very inefficient, remember that most programs spend something like 90% of their time in less than 10% of the code in the program, so unless the piece of code you’re thinking about happens to fall into that 10% it isn’t going to be important. Add Comment

“Premature optimization is the root of all evil.” is sometimes referred to as “Knuth’s law” (from Donald E. Knuth). Add Comment

Context and composition

One of the terms you will see used over and over in design patterns literature is context. In fact, one common definition of a design pattern is “a solution to a problem in a context.” The GoF patterns often have a “context object” that the client programmer interacts with. At one point it occurred to me that such objects seemed to dominate the landscape of many patterns, and so I began asking what they were about. Add Comment

The context object often acts as a little façade to hide the complexity of the rest of the pattern, and in addition it will often be the controller that manages the operation of the pattern. Initially, it seemed to me that these were not really essential to the implementation, use and understanding of the pattern. However, I remembered one of the more dramatic statements made in the GoF: “prefer composition to inheritance.” The context object allows you to use the pattern in a composition, and that may be its primary value. Add Comment


[1] This is a tongue-in-cheek reference to an event in China after the death of Mao-Tze Tung, when four persons including Mao’s widow made a power play, and were demonized by the Chinese Communist Party under that name.

[2] These same people were also convinced that all the computers were going to crash then, too. But since virtually everyone had the experience of their Windows machine crashing all the time without particularly dire results, this didn’t seem to carry the same drama of impending doom.

[ Previous Chapter ] [ Table of Contents ] [ Index ] [ Next Chapter ]
Last Update:12/31/2001