How to "cast" an object to a derived class?

Dieter Maurer dieter at handshake.de
Sun Sep 19 12:22:55 EDT 2021


Robert Latest wrote at 2021-9-18 13:03 GMT:
>Stefan Ram wrote:
>> Robert Latest <boblatest at yahoo.com> writes:         But how can I "promote" a
>>>given Opaque instance to the derived class?
>>
>>   Sometimes, one can use containment instead of inheritance.
>
>Nah, doesn't work in my case. I'm trying to write a wrapper around
>xml.etree.ElemenTree and .Element  to circumvent its idiotic namespace
>handling. For that I need inheritance since I want to override the find..()
>functions to return my derived MyElement classes. I think it could work but I
>somehow need to convert the root Element to a MyElement.

If the class is a pure Python class, then the `__class__` attribute
of its instances is writable. You could try to assign the
derived class -- at your own risk.



--
Dieter


More information about the Python-list mailing list