I'm a python addict !
bruno.42.desthuilliers at websiteburo.invalid
Tue Jan 27 10:21:36 CET 2009
Paul McGuire a écrit :
> On Jan 26, 10:54 am, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
>> On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
>>> Want to change the type/behavior of an object from class A to class
>>> B? How about this:
>>> aobj = A()
>>> aobj.__class__ = B
>>> Try *that* in as simple-looking C++ or Java!
>> Wow. That looks very powerful and fun. But scary. Any thoughts on how
>> you would use that in a way that wouldn't unleash sulphurous code
> This technique is perfect for implementing the GoF State pattern.
Except when it is not !-)
I tried twice to use this trick in "real life" code, and in both cases,
it ended up being much more complicated than implementing the state
pattern the canonical way. Not to say that it's hopeless - just that
there's the usual difference between theory and practice.
More information about the Python-list