I testing IronPython 2.0 and trying to integrate with my VB.NET application.

I would like to create an object to emulate a numeric type implementing the functions *__add__, **__radd__ , **__sub__ , **__rsub__ , etc *as explained in Python manual

http://www.python.org/doc/2.5.2/ref/numeric-types.html

I would like to create this emulated object inside my vb.net application and import this object from a python code like the example below

#IronPython code

#Vb.NET object
from IronPytonTest import TestNumericObject

# My numeric object with value 1
test=TestNumericObject(1)

print float(test+1) #print 2 ( 1 + 1 ) OK , It´s called the function __add__ from my vb code

print float(test+test) #print 2 ( test + test = 1+1 ) OK , It´s called the function __add__ from my vb code

print float(1+test) # Error: Microsoft.Scripting.ArgumentTypeException: unsupported operand type(s) for +: 'int' and 'TestNumericObject'
#__radd__ function is not called

Below is my vb code , basically I implemented the functions __radd__, __add__ in my vb.net code and trying to sum my objects inside python, but __radd__ is not called.

Public Class TestNumericObject

   Sub New(ByVal value As Double)
       m_value = value
   End Sub

   Private m_value As Double
   Public ReadOnly Property Value() As Double
       Get
           Return m_value
       End Get
   End Property

   Public Function __float__() As Double
       Return m_value
   End Function

   Public Function __add__(ByVal other As Object) As TestNumericObject
       Dim v As Double
       If TypeOf other Is TestNumericObject Then
           v = CType(other, TestNumericObject).m_value
       Else
           v = CDbl(other)
       End If
       Dim o As New TestNumericObject(0)
       o.m_value = Me.m_value + v
       Return o
   End Function

   Public Function __radd__(ByVal other As Object) As TestNumericObject
       Dim v As Double
       If TypeOf other Is TestNumericObject Then
           v = CType(other, TestNumericObject).m_value
       Else
           v = CDbl(other)
       End If
       Dim o As New TestNumericObject(0)
       o.m_value = Me.m_value + v
       Return o
   End Function

End Class






_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to