Design Patterns and Python

May 28th, 2007 § 4 comments

Daniel Nouri made a post this week­end enti­tled “Pat­terns, sim­plic­ity and sim­ple pat­terns” — it came at a good time. One of my cur­rent side projects (besides infant project plan­ning, ship­ping stuff at work and other stuff). Is to sit down with the Design Pat­terns and Head First Design Pat­terns and imple­ment each of the pat­terns in Python.

Then, I plan on back-tracking on myself and doing it again in Java.

Daniel’s post how­ever, makes some good points (brought up by Alex Martelli in “Five Easy Pieces: Sim­ple Python Non-Patterns”) — some of the Soft­ware Design pat­terns peo­ple have really come know “know and love” are baked into the low­est parts of the Python lan­guage itself.

From Daniel:

The most impor­tant point that Martelli makes in this paper is that in Python, some of the tra­di­tional soft­ware design pat­terns don’t apply, because the Python pro­gram­mer has facil­i­ties that other OO lan­guages lack, the lack of which makes cer­tain pat­terns nec­es­sary in other lan­guages that seem super­flu­ous in Python.

I’ve already run into this — not in design pat­terens per se — but in the nor­mal “Python is not Java/Perl/C*/etc” way. For instance, the age old strug­gle around sta­tic inter­faces. (See: Phillip J. Eby’s work “Python Is Not Java” and “Java is not Python, either…”)

I think imple­ment­ing each of the pat­terns (Why can’t we have Hobo Vis­i­tor as a pat­tern? You know, you add new meth­ods onto the object on the fly, and then they go away and take some­thing else with them?)

Fun­da­men­tally, some peo­ple hear “design pat­terns” and they run scream­ing, or they make a look like you just put some­thing in their cof­fee. I’m per­son­ally of the opin­ion that they’re some­thing you just need to know in pro­gram­ming — even if you don’t use them (or like me, use them with­out know­ing) every day.

Oh! And I almost for­got, Ryan Tomayko’s arti­cles “Getters/Setters/Fuxors” and “The Sta­tic Method Thing” are good reads as well.

ps: Also see Allen Holub — “Why get­ter and set­ter meth­ods are evil” and Eby’s “Python Inter­faces are not Java Inter­faces” and Ned Batchelder “Inter­faces”. 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.

  • Robert

    If you do all the design pat­terns in Python please post them for the masses to com­ment on and take a gan­der at. : )

  • http://www.jessenoller.com jesse

    Oh, I will post them for sure! Mainly so that peo­ple can point out how I have missed var­i­ous things.

  • Terry

    I started a thread a long while ago on headfirstlabs.com Head First Design Pat­terns on how to do stuff in Python.

    I got so frus­trated in just try­ing to do a cou­ple in Python that I just shelved the book. So Jesse, if you could post them, I’d be way into check­ing them out as a ref­er­ence so I can work my way through that book again.

    Keep up with the posts too. Excel­lent stuff.

  • http://www.jessenoller.com jesse

    @Terry: I will and I will! Ama­zon just deliv­ered my copy of Head First Design Pat­terns so between that and the main Pat­terns book I’m hop­ing I can get started once I unqueue.

What's this?

You are currently reading Design Patterns and Python at jessenoller.com.

meta