Changing an object's class

marshall marshall at spamhole.com
Sun May 25 23:15:07 EDT 2003


I am building a simple GUI and I want to determine the layout of
objects at runtime.  So I have stored the objects as a dictionary of
elements each of which is a dictionary of attributes including the
type.  Each type is a class.  I could query the type on load and
create each instance and load its attributes. But instead I used an
empty class and then changed it based on the type. [I'm sure there is
a comp sci name for this like meta-generator-abstract-whatnot - I call
it a Doppelganger.]

class cDoppel:
    def __init__(self,kw):
        self.__dict__ = kw
        self.__class__ = eval(kw['class'])

class cHalfBee:
    pass

class cPoofta:
    pass

class cContainer:
    def __init__(self,elements):
        for element in elements.items():
            name, attributes = element
            self.__dict__[name] = cDoppel(attributes)

#Test
elements = {'Eric':{'class':'cHalfBee','attribA':(1,2),'attribB':25},'Bruce':{'class':'cPoofta','ears':2}}
bag = cContainer(elements)
print 'Bag has',dir(bag)
print 'Eric has',dir(bag.Eric)
print 'Eric is a ' + str(bag.Eric)

Output:
Bag has ['Bruce', 'Eric', '__doc__', '__init__', '__module__']
Eric has ['__doc__', '__module__', 'attribA', 'attribB', 'class']
Eric is a <__main__.cHalfBee instance at 0x00C58BF0>

I like the results and might use this technique elsewhere.

Questions:
Is this naughty or nice?  Why?
Google shows some old, olympian discussion of locking down __class__
but I did not see a resolution of it.  Will this be a mortal or a
venal sin?

Thanks,
Marshall




More information about the Python-list mailing list