Live data from Hacker News

Design Patterns in Python (Free Book)

dpip.testingperspective.com

1–10 of 20 posts

Re: Design Patterns in Python (Free Book)

#3
post #2

Do we really need factory pattern in Python? I mean with all those dynamic calling methods? I'd like to see more patterns with decorators, list comprehension, map/filter chain and yield.

Absolutely. I have not seen a factory pattern in use in ages. I think the last time was before http://dirtsimple.org/2004/12/python-is-not-java.html was around.

Re: Design Patterns in Python (Free Book)

#4
post #2

Do we really need factory pattern in Python? I mean with all those dynamic calling methods? I'd like to see more patterns with decorators, list comprehension, map/filter chain and yield.

Absolutely. I have not seen a factory pattern in use in ages. I think the last time was before http://dirtsimple.org/2004/12/python-is-not-java.html was around.

Oooh, check out Django - it uses them for forms (and maybe other things). I've spent far too much time tinkering with modelformset_factory, inlineformset_factory, and my personal favourite, generic_inlineformset_factory.

(As a bonus, they're quite poorly documented too. Yay!)

Re: Design Patterns in Python (Free Book)

#5
Somehow, i'm not sure if i like the word "pattern". Patterns are something which can be used generally and repeatedly. I think that's sort of scary. For example: I've learned factory usage while doing twisted and it came naturally how and why this was used. But somehow i feel that with all those books and courses about "design patterns" new programers are missing out how to find a solution. It's like "here are your tools, MVC, factories and observers, try to solve your problems with those". This can be dangerous when a programer first looks how he can mold his problem to be solved with some pattern, whereas he should first analyse the problem and use patterns carefully where they fit naturally. I'm scared of a programming world where every bit is solved by factories of factories of metafactories ;)

Re: Design Patterns in Python (Free Book)

#6
These are Java design patterns. Python has different patterns. That's why people say that Factory is natural in Python.

Two examples, what i consider (very simple) Python patterns (without fancy names though):

    if __name__ == "__main__":
      ...


    def MyDecorator(func):
      def wrapper(...):
        def my_func(*args, **vargs):
           ...
        return my_func
      return wrapper

Re: Design Patterns in Python (Free Book)

#7
post #5

Somehow, i'm not sure if i like the word "pattern". Patterns are something which can be used generally and repeatedly. I think that's sort of scary. For example: I've learned factory usage while doing twisted and it came naturally how and why this was used. But somehow i feel that with all those books and courses about "design patterns" new programers are missing out how to find a solution. It's like "here are your t…

Design patterns are useful because they improve.. design. I mean application and code design. They model a recognizable structure. You are right: they have been sold as the ace of all trades in CS books and courses and they are not. That lead to the ' factories of factories of metafactories ' situation. Experience and intelligence are the real 'ace of all trades'. On the other side chaos is not good for anyone. Instead having a common lingo to identify code structure and common solutions to local architectural challenges is a good thing.

Re: Design Patterns in Python (Free Book)

#8
post #5

Somehow, i'm not sure if i like the word "pattern". Patterns are something which can be used generally and repeatedly. I think that's sort of scary. For example: I've learned factory usage while doing twisted and it came naturally how and why this was used. But somehow i feel that with all those books and courses about "design patterns" new programers are missing out how to find a solution. It's like "here are your t…

Design patterns are useful because they improve.. design. I mean application and code design. They model a recognizable structure. You are right: they have been sold as the ace of all trades in CS books and courses and they are not. That lead to the ' factories of factories of metafactories ' situation. Experience and intelligence are the real 'ace of all trades'. On the other side chaos is not good for anyone. Inste…

So, wouldn't it be better to write books on how someone got from a problem to a solution and why the outcome was something that can be described as a factory pattern, rather then the other way around?

I didn't read books on design patterns, but the book from the link structures around what design patterns there are and how they got applied to a problem. It'd be nicer to first have a problem and show why and how a particular pattern is better than, say, a bunch of monolithic lines of code.. So that you can say "oh and by the way, the way we solved that problem falls into the category of factory patterns".

Re: Design Patterns in Python (Free Book)

#9
I looked at couple of them. Some of the patterns has a note mentioning it is a port of C#/Java/C++ implementation in the Wikipedia article.

Python is drastically different from C#/Java/C++, and a direct port of classical patterns won't make much sense. It will work, but it will the equivalent of Fortran programmers writing Frotran in Python.

If anyone is interested, this talk by Alex Martelli is a good starting point:

    http://www.youtube.com/watch?v=0vJJlVBVTFg
I think Bruce Eckel was writing a book on "Design Patterns" in Python, but that's not completed. However, there is a book using Ruby, which shows the classical approach for implementing design patters, then shows how it is done in Ruby:

http://designpatternsinruby.com/

Re: Design Patterns in Python (Free Book)

#10
post #2

Do we really need factory pattern in Python? I mean with all those dynamic calling methods? I'd like to see more patterns with decorators, list comprehension, map/filter chain and yield.

> Do we really need factory pattern in Python?

Yes.

    from PIL import Image
    im = Image.open("bride.jpg")
Image.open here is a factory, and im will be an object of a different class, depending on the filename you pass to it(jpeg, gif, png).

That is just one example, but you get the idea.

Post reply on HN