[Tutor] Changing a class into a subclass
Alan Gauld
alan.gauld at freenet.co.uk
Thu Mar 24 23:10:15 CET 2005
> Are you referring to a Python object? If so you may assign a class
to the
> object's __class__ attribute.
> class A:pass
> class B:pass
> b = b()
> print b.__class__ # displays <class __main__.B at 0x011BB9C0>
> b.__class__ = A
> print b.__class__ # displays <class __main__.A at 0x011BBA20>
Interesting Bob, but presumably that doesn't change the internal
state so the OP would need to run an initialise method using
the XML? Or no, I suppose if he did this inside the class's
init method, as the first step, it would then initialise as
usual?
I feel a spell at the >>> coming on...
The problem is you still need to identify the class itself
(not the class name) before you can do this, but I guess you
might get that from either the builtins dictionary or the
Building module if it has all the Building subclasses in?
But it certainly does allow you to 'convert' from one class
to another, much like the C++ cast operator.
Alan g.
Learning something new every day.
More information about the Tutor
mailing list