Subclassing a win32com.client.Dispatch() Object?

Alex Martelli aleaxit at yahoo.com
Tue Aug 14 17:44:25 EDT 2001


<rcspython at yahoo.com> wrote in message
news:mailman.997816466.7215.python-list at python.org...
> Can this be done usign win32com?
> I have win32com.client.DispatchWithEvents("ACOMCLASS",ANEVENTCLASS)
>
> I need something like
>
> class myclass(?????):#What values are used for ?????
>   def __init__(????):
>     #What code goes here?
>
> myobj = myclass(win32com.client.DispatchWithEvents
> ("ACOMCLASS",ANEVENTCLASS))

the win32com.client.DispatchWithEvents appears to return
an instance of class win32com.client.EventProxy, so you could
subclass that class, if you wish.  However, it's unusual for
a class to be initialized by an instance of its superclass (and
that one only defines a few special methods basically
implementing automatic delegation, anyway).

You could try something like:

class myclass(win32com.client.EventProxy):
    def __init__(self, epwrapper):
        # steal the com object from the proxy
        self.__dict__['_obj_']=epwrapper.__dict__['_obj_']
        del epwrapper.__dict__['_obj_']
        try: del epwrapper
        except: pass
    # insert any additional/overriding methods you want,
    # but NOT __del__, __getattr__, __setattr__, which
     # you want to inherit from EventProxy

But this may be too low-level and fussy -- it's just an
attempt to avoid the repeated layers of auto-delegation
that it would, alas, be all to easy to get otherwise.


Alex






More information about the Python-list mailing list