[Tutor] Changing a class into a subclass
Bob Gailer
bgailer at alum.rpi.edu
Thu Mar 24 20:02:29 CET 2005
At 10:30 AM 3/24/2005, Max Noel wrote:
>On Mar 24, 2005, at 18:07, Ismael Garrido wrote:
>
>>Hello.
>>
>>I have a program that saves/loads to/from XML. I have a main class
>>Building, and a subclass House(Building). When I save the code I instruct
>>each object to save itself to XML (using ElementTree), so House adds
>>itself to the XML tree.
>>My problem is when I load the XML. Before having subclasses what I did
>>was to send the XML object (again in ElementTree) to the object and let
>>itself load. Now, with subclasses, if I send the House XML to Building I
>>get a Building instance, when I need a House instance.
>
> You can't make an object transform into an object of a different
> class
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>
[snip]
Bob Gailer
mailto:bgailer at alum.rpi.edu
510 558 3275 home
720 938 2625 cell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050324/feec2d7f/attachment.html
More information about the Tutor
mailing list