COM interfaces and inheritance

Alan Kennedy alanmk at hotmail.com
Tue Nov 19 07:43:06 EST 2002


Rob Sykes wrote:

> I'm having a problem with a (3rd party) IDispatch interface

> I'm struggling with the upcasting(?) - in Delphi there is
> newBar := CreateFoo() as bar
> 
> What is the Python equivalent ?

There is no casting in python. All resolution of attribute and method
names takes place at execution time. Consider the following function

def func(obj):
    print "About to call obj.meth()"
    obj.meth()
    print "Have called obj.meth()"

You can pass any class of object to this function, and the call will
succeed iff the object has a method called "meth". It doesn't matter
what class the object is, no casting is necessary. Consider these

class O:
    def meth(self):
        print "In class O"

class P: # Unrelated to class O
    def meth(self):
        print "In class P"

class Q: # Unrelated to O or P
    def nometh(self):
        print "In class Q"

>>> o = O()
>>> func(o)
About to call obj.meth()
In class O
Have called obj.meth()
>>> p = P()
>>> func(p)
About to call obj.meth()
In class P
Have called obj.meth()
>>> q = Q()
>>> func(q)
About to call obj.meth()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in func
AttributeError: Q instance has no attribute 'meth'

I will post separately on your COM objects problem.

regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan



More information about the Python-list mailing list