Type casting a base class to a derived one?

Frederic Rentsch anthra.norell at vtxmail.ch
Mon Jan 15 15:07:07 EST 2007


Chris Mellon wrote:
> On 11 Jan 2007 15:01:48 +0100, Neil Cerutti <horpner at yahoo.com> wrote:
>   
>> On 2007-01-11, Frederic Rentsch <anthra.norell at vtxmail.ch> wrote:
>>     
>>> If I derive a class from another one because I need a few extra
>>> features, is there a way to promote the base class to the
>>> derived one without having to make copies of all attributes?
>>>
>>> class Derived (Base):
>>>    def __init__ (self, base_object):
>>>       # ( copy all attributes )
>>>       ...
>>>
>>> This looks expensive. Moreover __init__ () may not be available
>>> if it needs to to something else.
>>>
>>> Thanks for suggestions
>>>       
>> How does it make sense to cast a base to a derived in your
>> application?
>>
>>     
>
> I can't figure out any circumstance when you'd need to do this in
> Python. Upcasting like this is something you do in statically typed
> languages. I suspect that the OP doesn't really believe dynamic
> casting works and doesn't want to pass a derived class for some
> reason.
>   
What for? If an instance needs to collect a substantial amount of data 
and needs to perform a substantial amount of processing in order to 
analyze that data, and if the appropriate type of the instance depends 
on the analysis, I thought that the instance might at that point just 
kind of slip into the appropriate identity.
      After studying the various helpful suggestions, some of which, 
like this one, question the wisdom of such an approach, I think I see 
the light: I'd have a class that does the collecting and the analyzing, 
or even two classes: one collecting the other analyzing and then, 
depending on the outcome of the analysis, make the appropriate processor 
and hand it the data it needs. Right?
     
Thank you all very much for your input.

Frederic (OP)





More information about the Python-list mailing list