do design patterns still apply with Python?

Roy Smith roy at panix.com
Thu Mar 2 22:38:44 CET 2006


In article <8SINf.1718$No6.40137 at news.tufts.edu>,
 John Salerno <johnjsal at NOSPAMgmail.com> wrote:

> Since Python does so many things different, especially compared to 
> compiled and statically typed languages, do most of the basic design 
> patterns still apply when writing Python code? If I were to read a 
> design pattern book (such as Head First Design Patterns), could I apply 
> their Java examples to Python easily enough, or does Python require a 
> different perspective when applying patterns?

Many of the classic design patterns apply just fine to Python, at least in 
the high-level view.  On the other hand, much (most?) of what's in the 
classic design pattern books is so tied up with ways around C++/Java type 
bondage, it's difficult to see the forest for the trees.

For example, take the most classic of all patterns, Singleton.  A typical 
C++ Singleton treatment will be all about making constructors private and 
shit like that.  None of that carries over to Python.  What I would do in 
Python is have a module-level factory function which caches a single 
instance of the class to return to the 2nd and subsequent caller and not 
get my panties in a twist over the fact that some clever programmer could 
find away around my code and force creation of a second instance.

The basic concepts in the pattern books are worth knowing.  You just have 
to be able to tease apart the concepts from the language-specific cruft 
that so often obfuscates the descriptions.



More information about the Python-list mailing list