Patterns in Python
suttoncourtenay.org.uk
suttoncourtenay.org.uk
I'd also take issue with the implication that Python is powerful enough for the common patterns to disappear completely. it doesn't have macros, for a start, so implementing coupled Strategies of any complexity means you're back in class FooStrategy() land.
Recognising when you are or should be implementing a recognised pattern means that you can rely on a lot of analysis of that pattern, largely without paying attention to the environment in which that analysis was performed. That's potentially a big win.
I guess what I'm getting at is that throwing away patterns means throwing away a decent lingua franca for talking about the anatomy of your code. Given that, as someone far smarter than me once said, code is primarily for humans to read and only incidentally for computers to execute, that seems like a poor decision.
The code in this article confirms the suspicion of pattern skeptics that GoF-style patterns lead to code constructed out of pick-and-mix patterns instead of actual sensible code.
I don't think pointing someone who does not realize that the strategy pattern can be accomplished by passing around functions since they are first class objects at a dynamic language DP tutorial is going to do anything more than give them different ideas to misuse.
However, I've since come to believe that it's not the ideas that are harmful, it's the people that use them without comprehending them. If this is the case, avoiding a concept that would provide benefit is limiting yourself without any benefit as the people that would misuse them will probably find something else to screw up anyway.
Or, to put it more succinctly: "You can write Fortran in any language"
View document source. Generated on: 2003-02-28 11:59 UTC.
I was thinking I'd seen this before -- a long time ago. How much still applies?
Design pattern are conceptual implementation strategies and not components, therefore only fundamental language paradigm changes could (should?) make them no longer applicable for the most part.
Python may not "need" these patterns, but python programmers might. At the minimum, I find it useful to know how design patterns from Java, Smalltalk, etc. are done in Python, especially if it gives me bragging rights that a pattern can be implemented in a fraction of the LoC needed for those other languages.
This guy doesn't do it here, but you can make singletons relatively painless in Python by using decorators.
http://www.python.org/dev/peps/pep-0318/
E.g. 2/28/2003 vs 6/5/2003
So, upvoted.
This is not amenable to inheritance tho, however there is a strong argument against inheritance in singletons anyway. Further, the borg pattern is considered "more pythonic" in the case of inheritable singletons, as frequently what is desired in such cases is shared state vs instead of shared identity.
In the general case, yes. Unfortunately, due to some quirks in the module loader, assigning bar right inside foo might not yield the expected result, though.
This is actually one of the python warts that annoys me the most.
Just don't reload it.
from foo import bar