[Ironpython-users] Implicit conversion of objects to float

Cesar Mello cmello at gmail.com
Thu Mar 1 21:13:13 CET 2012


Hey guys!

Just curious: is there a way to implement in the .NET class something that
maps to the ** operator?

I've tried the other way: calling into a Python function that works as a
factory of proxy objects. These proxies overload the arithmetic operators
and delegates everything to the real (.NET) class. The user is given access
to the proxies in the expressions instead of the real objects. It works,
but performance suffers. (My performance target right now is 20.000
expression evaluations / second).

Thank you very much for the attention!

Best regards
Mello

On Fri, Feb 24, 2012 at 5:17 PM, Jeff Hardy <jdhardy at gmail.com> wrote:

> On Fri, Feb 24, 2012 at 12:00 PM, Cesar Mello <cmello at gmail.com> wrote:
> > Thank you very much for the quick response Jeff!
> >
> > First, let me clarify I am a Python newbie, so my assumptions about
> Python
> > may be all wrong.
> >
> > I had tried __float__ in a Python object, but it does not work implicitly
> > inside expressions (and I think that's the correct behavior). You still
> have
> > to use float(a) for the conversion to be used.
> >
> > Now I implemented the C# implicit conversion to double() and I get the
> same
> > behavior (it works if I use float(a) in the expression but if I use a *
> 5.0
> > for example I get the error: "unsupported operand type(s) for *:
> 'DataValue'
> > and 'float'.
>
> Ah, you missed this first part: you'll need to overload the arithmetic
> operators for your objects.
>
> Python: Define __add__, __sub__, etc.
> (http://docs.python.org/reference/datamodel.html#object.__add__)
> C#: Define operator+, operator-, etc.
> (http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx)
>
> - Jeff
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120301/d613a2d4/attachment.html>


More information about the Ironpython-users mailing list