COM interfaces and inheritance

Alex Martelli aleax at aleax.it
Tue Nov 19 08:17:04 EST 2002


Rob Sykes wrote:

> Hi all,
> 
> I'm having a problem with a (3rd party) IDispatch interface
> 
> The problem can be summarised thus: I have two classes, one
> inheriting from the other.
> 
> class foo():
>     pass
> 
> class bar(foo):
>     self.stuff = 7

Invalid Python on several scores.

 
> I need to create a new bar object but the 3rd party interface gives
> me a method which returns foo objects
> 
> newBar = CreateFoo() # newBar should be a bar but CreateFoo returns
>                      # a foo
> 
> I'm struggling with the upcasting(?) - in Delphi there is
> 
> newBar := CreateFoo() as bar
> 
> What is the Python equivalent ?

you can try newBar.__class__ = bar, but it's quite unlikely that
this what do whatever it is that you're really after.

The "third party dispatch interface" is really returning pointers
to COM interfaces.  What you use in Python are wrappers around
those.  Getting a different interface (assuming the underlying
COM object supports it) is a matter of COM's QueryInterface, but
as long as you're accessing things through dispatch (which, from
Python, you mostly are) that's quite unlikely to matter at all.
Getting a different wrapper you can do in other ways.  The two
things are rather unrelated.

So, unless you give more and more-Pythonic details of what isn't
currently working for you, and how you'd like it to work instead,
it's unlikely that we can be very useful in our suggestions.


Alex




More information about the Python-list mailing list