Return class instance from COM method in Python

Paul paul.kemp at standardbank.com
Tue Oct 21 08:14:16 EDT 2003


I managed to solve my own problem - it didn't like having two __init__
methods (different prototypes).

However, I have another problem.  I've worked out how to return a
list, and also how to return an instance of a class.  I know need to
be able to return a list of objects.  Is this possible (no luck so
far)?

Paul

paul.kemp at standardbank.com (Paul) wrote in message news:<bb8e48c9.0310200804.78350aef at posting.google.com>...
> Hi
> 
> I'm implementing a few COM classes using Python.  I've come across a
> problem when I'm trying the return an instance of a class from a
> method on one of my classes.  The code is as follows, with two classes
> - clsDividend and clsDividendEsimate.  The
> clsDividend.GetDividendEstimates method tries to return a new instance
> of the clsDividendEstimate class, but throws an error.
> 
> Any ideas what I'm doing wrong - is it possible to do what I'm trying
> to achieve?
> 
> The client will ultimately be Excel, but I can't even get it to work
> within a Python client yet.  Ultimately I'd like to do this in my
> client(pseudo):
> 
> oDiv = CreateObject("SBLFront.clsDividend")
> oDivEst = oDiv.GetDividendEstimates("testval")
> msgbox oDivEst.ccy
> 
> Many thanks
> Paul
> 
> # Expose the Python SBLFront.
> class clsDividend(clsSBLFront):
>     """The SBLFront object exposed via COM
>     """
>     #_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
>     _public_methods_ = ['GetDividendEstimates']
>     _public_methods_.extend(clsSBLFront._public_methods_)
> 
>     # All registration stuff to support fully automatic
> register/unregister
>     _reg_verprogid_ = "SBLFront.clsDividend.1"
>     _reg_progid_ = "SBLFront.clsDividend"
>     _reg_desc_ = "Python SBLFront"
>     _reg_clsid_ = "{2E851A1B-A8D1-4AD5-BA16-EB6B9BCF2F21}"
>     _reg_class_spec_ = "win32com.servers.sblfront.clsDividend"
> 
>     # ------------------------------------------------------------------------
> 
>     def __init__(self):
>         clsSBLFront.__init__(self)
>         self.dict = {}
> 
>     def GetDividendEstimates(self, InsId):
>         oDivEst = clsDividendEstimate('USD', 1.0, 1.0,
> '2003-01-01','2003-01-01','2003-01-01','Test description')
>         return oDivEst        
> 
> # ----------------------------------------------------------------------------
> 
> class clsDividendEstimate:
>     """The SBLFront object exposed via COM
>     """
>     _public_methods_ = []
>     _public_attrs_ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
> 'ex_div_day', 'day', 'description']
>     _readonly_attrs_ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
> 'ex_div_day', 'day', 'description']
>     
>     # All registration stuff to support fully automatic
> register/unregister
>     _reg_verprogid_ = "SBLFront.clsDividendEstimate.1"
>     _reg_progid_ = "SBLFront.clsDividendEstimate"
>     _reg_desc_ = "Python SBLFront"
>     _reg_clsid_ = "{055DEFBC-B095-4C5B-A9CE-BFBB965BBDC1}"
>     _reg_class_spec_ = "win32com.servers.sblfront.clsDividendEstimate"
> 
>     # ------------------------------------------------------------------------
> 
>     def __init__(self):
>         self.dict = {}
>         self.ccy = ''
>         self.dividend = 0.0
>         self.tax_factor = 0.0
>         self.pay_day = ''
>         self.ex_div_day = ''
>         self.day = ''
>         self.description = ''        
> 
>     def __init__(self, sCCY, dDiv, dTax, dtDay, dtPayDay, dtExDivDay,
> sDesc):
>         self.dict = {}
>         self.ccy = sCCY
>         self.dividend = dDiv
>         self.tax_factor = dTax
>         self.pay_day = dtPayDay
>         self.ex_div_day = dtExDivDay
>         self.day = dtDay
>         self.description = sDesc




More information about the Python-list mailing list