[IronPython] Emulating numeric type in C# or VB.net , __radd__ is not called

David DiCato ddicato at microsoft.com
Mon Jun 8 23:11:58 CEST 2009


Hmm, the fact that we aren't picking up __radd__ properly looks like a bug, which I'll go ahead and log on CodePlex. The good news is that there's a more common usage pattern that works correctly.

The canonical way to do this is to define + and - as static (Shared in VB speak) operator methods. These should automatically manifest themselves as __add__, __radd__, __sub__, and __rsub__ in IronPython. Specifically, I recommend trying something like this:

Public Structure NumericObject
                ...

                ' This method should become __add__
Public Shared Operator +(ByVal self As NumericObject, ByVal other As Object) _
                ...
End Operator

' This method should become __radd__
Public Shared Operator +(ByVal other As Object, ByVal self As NumericObject) _
                Return self + other
End Operator

' Ditto with subtraction
...
End Structure

In addition, if you're only interested in adding/subtracting objects of the same type, you need only define each operator once:

Public Structure NumericObject
                ...

                ' This method should become __add__ and __radd__
Public Shared Operator +(ByVal self As NumericObject, ByVal other As NumericObject) _
                ...
End Operator

' Ditto with subtraction
...
End Structure

I've only tested this in C# against IPy 2.6, so please let me know how it turns out. Good luck!
- David

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ricardo
Sent: Sunday, June 07, 2009 7:26 PM
To: users at lists.ironpython.com
Subject: [IronPython] Emulating numeric type in C# or VB.net , __radd__ is not called

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





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090608/1b019c40/attachment.html>


More information about the Ironpython-users mailing list