Design Patterns and Python

by jesse in ,

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.