win32com: subclass a com object?

Mark Hammond mhammond at skippinet.com.au
Sun Dec 12 17:46:42 EST 1999


tiddlerdeja at my-deja.com wrote in message <830pi6$n4k$1 at nnrp1.deja.com>...

>I'd like to know how to subclass or derive from a existing COM object.
>This subclass would implemented as a COM object itself. The "create a
>COM object in python" I can do. It's just the inheritance I can't. I'd
>like then to override a method of the base class in the derived class.
>(I have run makepy.py over the existing/base COM object already).

Another reply suggested using delegation - ie, create a completely seperate
class, and have it hold the "real" COM object.  This is a good suggestion.

However, to implement sub-classing you can do the following:
>>> klass = win32com.client.gencache.GetClassForProgId("Excel.Application")
>>> if klass is None: raise RuntimeError, "Please run makepy for this
object."
>>> class MyExcel(klass):
...    pass

Mark.






More information about the Python-list mailing list