Hi, Cesar,

In C#, there's one difference in the following line:
      double sum = val1 + val2;

Here, the compiler knows that the result of the expression should be double, 
and so it can search for implicit cast operators to double.

In python, you just write
        sum = val1 + val2

So the compiler does not know that the result of the expression should be 
double, because sum has no type.

Imagine you have implicit conversion operators to int, double and string. All 
of them provide an + operator. Which of them do you want the compiler to apply?

Best regards

Markus Schaber
-- 
___________________________
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 | Fax 
+49-831-54031-50

Email: m.scha...@3s-software.com | Web: http://www.3s-software.com 
CoDeSys internet forum: http://forum.3s-software.com
Download CoDeSys sample projects: 
http://www.3s-software.com/index.shtml?sample_projects

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner | Trade 
register: Kempten HRB 6186 | Tax ID No.: DE 167014915 

Von: ironpython-users-bounces+m.schaber=3s-software....@python.org 
[mailto:ironpython-users-bounces+m.schaber=3s-software....@python.org] Im 
Auftrag von Cesar Mello
Gesendet: Samstag, 25. Februar 2012 17:25
An: Jeff Hardy
Cc: ironpython-users@python.org
Betreff: Re: [Ironpython-users] Implicit conversion of objects to float

Hi,

Although implementing the arithmetic operator overloads solve my problem, I am 
still curious if there is a way to implement the implicit conversion in Python, 
much like the sample C# program below does. This is a question about the 
language, so please forgive me if I am asking in the wrong mailing list.

class DataValue
  {
    public double Value { get; set; }
 
    public DateTime Timestamp { get; set; }
 
    public static implicit operator double(DataValue dataValue)
    {
      return dataValue.Value;
    }
  }
 
  class Program
  {
    static void Main(string[] args)
    {
      DataValue val1 = new DataValue { Value = 2.5 };
      DataValue val2 = new DataValue { Value = 3.5 };
 
      double sum = val1 + val2;
      Console.WriteLine(sum.ToString());
    }
  }

Notice I did not need to implement the operator+ overload, and this object can 
be used anywhere a double is. I suppose this can be done with __coerce__ in 
Python 2, but that is not recommended in the documentation (and was removed in 
Python 3).  Is there any other way to obtain this behavior in Python?

I agree it may be error-prone. But there are valid scenarios where it is not. 
Although implementing the arithmetic overloads allow me to mix DataValues and 
floats in the same expressions, I am not able to initialize a Python's Decimal 
with a DataValue, for example. In C# that could be done.

Thank you very much for the attention!

Best regards
Mello

On Fri, Feb 24, 2012 at 7:50 PM, Cesar Mello <cme...@gmail.com> wrote:
OK thank you very much!

Best regards
Mello

On Fri, Feb 24, 2012 at 6:17 PM, Jeff Hardy <jdha...@gmail.com> wrote:
On Fri, Feb 24, 2012 at 12:00 PM, Cesar Mello <cme...@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


_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to