Design Patterns and Python
Daniel Nouri made a post this weekend entitled "Patterns, simplicity and simple patterns" - it came at a good time. One of my current side projects (besides infant project planning, shipping stuff at work and other stuff). Is to sit down with the Design Patterns and Head First Design Patterns and implement each of the patterns in Python.
Then, I plan on back-tracking on myself and doing it again in Java.
Daniel's post however, makes some good points (brought up by Alex Martelli in "Five Easy Pieces: Simple Python Non-Patterns") - some of the Software Design patterns people have really come know "know and love" are baked into the lowest parts of the Python language itself.
From Daniel:
The most important point that Martelli makes in this paper is that in Python, some of the traditional software design patterns don't apply, because the Python programmer has facilities that other OO languages lack, the lack of which makes certain patterns necessary in other languages that seem superfluous in Python.
I've already run into this - not in design patterens per se - but in the normal "Python is not Java/Perl/C*/etc" way. For instance, the age old struggle around static interfaces. (See: Phillip J. Eby's work "Python Is Not Java" and "Java is not Python, either...")
I think implementing each of the patterns (Why can't we have Hobo Visitor as a pattern? You know, you add new methods onto the object on the fly, and then they go away and take something else with them?)
Fundamentally, some people hear "design patterns" and they run screaming, or they make a look like you just put something in their coffee. I'm personally of the opinion that they're something you just need to know in programming - even if you don't use them (or like me, use them without knowing) every day.
Oh! And I almost forgot, Ryan Tomayko's articles "Getters/Setters/Fuxors" and "The Static Method Thing" are good reads as well.
ps: Also see Allen Holub - "Why getter and setter methods are evil" and Eby's "Python Interfaces are not Java Interfaces" and Ned Batchelder "Interfaces". All in all both Python and Java have pros, cons and things that make you go ewwww... But you should always use the right tool for the right job.


May 28th, 2007 at 11:41 pm
If you do all the design patterns in Python please post them for the masses to comment on and take a gander at. : )
May 28th, 2007 at 11:51 pm
Oh, I will post them for sure! Mainly so that people can point out how I have missed various things.
May 30th, 2007 at 7:36 pm
I started a thread a long while ago on headfirstlabs.com Head First Design Patterns on how to do stuff in Python.
I got so frustrated in just trying to do a couple in Python that I just shelved the book. So Jesse, if you could post them, I’d be way into checking them out as a reference so I can work my way through that book again.
Keep up with the posts too. Excellent stuff.
May 30th, 2007 at 7:44 pm
@Terry: I will and I will! Amazon just delivered my copy of Head First Design Patterns so between that and the main Patterns book I’m hoping I can get started once I unqueue.