I'm a python addict !

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jan 26 20:41:25 CET 2009


J. Cliff Dyer wrote:
> On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
>> ... How about this:
>>     aobj = A()
>>     aobj.__class__ = B
> ... 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?

Seems like a nice way to transition between "ActiveAccount": and
"InActiveAccount" or some such.  The requirement is that the two
classes share memory layout (essentially, class type and all __slots__
involved (including __dict__).  It is a bit prickly, so I'd personally
only start to use it once my methods were littered with lots of:
     ...
     if self.active_flag:
         self.account.update(...
     else:
         raise ValueError('Purchase prohibitted')

Could also be useful as the stub for a kept-on-disk structure,
where all methods on the inactive one rolled in data switched class,
and invoked their corresponding method.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list