Paul McGuire a écrit :
On Jan 26, 10:54 am, "J. Cliff Dyer" <j...@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
smells?


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.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to