Python Patterns

Irmen de Jong irmen at
Wed Oct 6 00:40:13 CEST 2004

Alex Martelli wrote:
>  Meanwhile Robert Martin has already written a
> book far closer than any other to what I had in mind -- but he uses C++
> and Java as explanatory languages, and _that_ will give MY book enough
> of an edge to almost catch up with Uncle Bob's awesome experience and
> writing skills...!-)

A bit of searching found this book:
"Agile Software Development, Principles, Patterns, and Practices" ?

>>This "state" pattern needs a clumsy internal state object in C++
>>but in Python we can just change the object's class directly,
>>which is a much more direct implementation of what the pattern
>>tries to achieve :-)
> Right, good point.  In C++ you can do something closer with the Strategy
> pattern -- still one level of indirectness, but a consistent set of
> methods (embodied in the one strategy object) commuted as a whole.
> Assigning __class__ IS of course more direct, yes;-).

I just flipped the page in the GOF DP book, having read about "State",
and there it was: "Strategy". Interestingly enough, the only difference
-at first glance- with "State" is that strategy's 'Context' object
(the object that contains the reference to the strategy object),
has a different interface than with the State pattern. With state it
is essentially identical to the internal state object -- which,
in Python, is just the new class we assign to the object ;-)

State is about changing an object's class at runtime, to make it do
something different when the object is "in a different state".
Strategy is about being able to switch or add internal algorithms
without having to change the code that uses it.
So say the GOF. Amen.   :-D

Don't know why I'm writing all this. I have some reading to do...


More information about the Python-list mailing list