def WithEvents(disp, user_event_class):
 disp = Dispatch(disp)
 if (not disp.__class__.__dict__.get('CLSID')):
  try:
   ti = disp._oleobj_.GetTypeInfo()
   disp_clsid = ti.GetTypeAttr()[0]
   (tlb, index) = ti.GetContainingTypeLib()
   tla = tlb.GetLibAttr()
   gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4], bValidateFile=0)
   disp_class = gencache.GetClassForProgID(str(disp_clsid))
  except pythoncom.com_error:
   raise TypeError('This COM object can not automate the makepy process - please run makepy manually for this object')
 else:
  disp_class = disp.__class__
 clsid = disp_class.CLSID
 try:
  from types import ClassType as new_type
 except ImportError:
  new_type = type
 events_class = getevents(clsid)
 if (events_class is None):
  raise ValueError('This COM object does not support events.')
 result_class = new_type('COMEventClass', (events_class, user_event_class), {})
 instance = result_class(disp)
 if hasattr(user_event_class, '__init__'):
  user_event_class.__init__(instance)
 return instance

def WithEvents ( disp , user_event_class ) : disp = Dispatch ( disp ) if ( not disp . __class__ . __dict__ . get ( 'CLSID' ) ) : try : ti = disp . _oleobj_ . GetTypeInfo ( ) disp_clsid = ti . GetTypeAttr ( ) [ 0 ] ( tlb , index ) = ti . GetContainingTypeLib ( ) tla = tlb . GetLibAttr ( ) gencache . EnsureModule ( tla [ 0 ] , tla [ 1 ] , tla [ 3 ] , tla [ 4 ] , bValidateFile = 0 ) disp_class = gencache . GetClassForProgID ( str ( disp_clsid ) ) except pythoncom . com_error : raise TypeError ( 'This COM object can not automate the makepy process - please run makepy manually for this object' ) else : disp_class = disp . __class__ clsid = disp_class . CLSID try : from types import ClassType as new_type except ImportError : new_type = type events_class = getevents ( clsid ) if ( events_class is None ) : raise ValueError ( 'This COM object does not support events.' ) result_class = new_type ( 'COMEventClass' , ( events_class , user_event_class ) , { } ) instance = result_class ( disp ) if hasattr ( user_event_class , '__init__' ) : user_event_class . __init__ ( instance ) return instance
similar to dispatchwithevents - except that the returned object is *not* also usable as the original dispatch object - that is the returned object is not dispatchable .