[IronPython] Passing arguments to C#

Richard Nienaber rjnienaber at gmail.com
Wed Dec 29 00:39:17 CET 2010


Hi Mark

I managed to find the
following<http://lists.ironpython.com/pipermail/users-ironpython.com/2006-June/002583.html>in
the mailing list archives which seems to indicate that it was a design
decision:

We thought the implicit conversion from a List to an array was a little
> evil. The array could be updated inside of the call and the user's list
> wouldn't be updated. A caller w/ a tuple doesn't expect to see their tuple
> updated, and therefore that is an acceptable conversion.
>

As is suggested in the mail, using a tuple does seem to work:

C#:
using System;

public class DoubleTest {
    public void M(double[] arg) { Console.WriteLine(arg[1]); }
}

IronPython 2.6:
import DoubleTest
o = DoubleTest()
values = tuple([1, 5])
o.M(values)      #prints '5'


Even though converting to a tuple does work, I think providing a wrapper
that accepts IList (which is one of your suggestions) would provide a better
experience for the users of your api.

Richard


On Tue, Dec 28, 2010 at 10:33 PM, Mark Senko <msenko at completegenomics.com>wrote:

>  I’m trying to set up a simple scripting environment for non-programmers
> that can call into our C# codebase.
>
> Python is a non-typed language, whereas C# is strongly typed.  This is one
> of the reasons IronPython was written.
>
>
>
> It seems that the solution presented by IronPython is to expose .NET types
> into Python.  If I want to call a C# function that takes an array of doubles
> as an argument, I have a problem.
>
>
>
> I know you can do something like:
>
> Import System
>
> Array=System.Array[float](range(10))
>
> To create and initialize the array with a sequence of numbers.  But now you
> are working with a .NET variable, not a Python variable.
>
> I can also do something similar with lists.
>
>
>
> I don’t want to bring variable typing into Python, because then it’s not
> Python.  I also don’t want my users to have to worry about this extra typing
> and keeping straight which variables need it and which don’t.
>
> I think the IronPython.Runtime should take care of this, but it doesn’t.
>
>
>
> I’m willing to write wrappers, in C#, for all the functions (if needed)
> that I want to expose to Python. I’ve been knocking myself out trying to do
> this.
>
> I’ve been trying to accept a non-typed list, and use it to fill an array of
> doubles.  In my trial and error attempts, I’ve prototyped my argument as
>
> Public static void TestIt(IList inList) and public static void
> TestIt(IronPython.runtime.List inList).  They both act pretty much the same.
> I’ve also tried using tuples.
>
>
>
> If my python list is created with  pyList=range(10), then passed as an
> argument
>
> No matter what I do, I’ve been unsuccessful filling my array of doubles
> using
>
>   myDoubles[i] = (double)inList[i];  (complains that it can’t cast a value
> of infinity)
>
> or any other type of casting I can come up with.
>
> The debugger shows a nice list, with values 0-9.
>
> inList[i].GetType() shows System.Int32.  So why can’t it cast?
>
>
>
> As an aside, if I start with pyList=map(float,range(10)), then  GetType()
> returns System.double and the cast works without a problem(and probably
> isn’t needed).
>
> But this requires the user to once again worry about the variable type.
>
> Based on this, I’m pretty sure if I set up a switch statement on type in my
> wrapper, and assign a temp variable with the same type, then the cast of the
> temp variable it would work.
>
> But this sure seems clumsy!
>
>
>
> Does anyone have an idea of how to do this?  I’m sure I just haven’t hit on
> the right combination.
>
> Basically I want the user to be able to use pyList = range(10) and let me
> worry about converting it to double in a C# wrapper if that’s what is
> needed.
>
>
>
> I’m already resigned to writing wrappers in Python to hide
> clr.Reference[type]() from my users when calling a C# functions that
> requires an “out” or “ref” parameters.
>
>
>
> Thanks in advance for reading a post that I **know** is as much rant as
> question.  It’s just been frustrating ….
>
>
>
>
>
>
>
> *Mark Senko*
>
> Complete Genomics, Inc.
>
> 2071 Stierlin Court
>
> Mountain View, CA 94043
>
>
>
> ____
>
> The contents of this e-mail and any attachments are confidential and only for
> use by the intended recipient. Any unauthorized use, distribution or copying
> of this message is strictly prohibited. If you are not the intended recipient
> please inform the sender immediately by reply e-mail and delete this message
> from your system. Thank you for your co-operation.
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20101228/7fb292c3/attachment.html>


More information about the Ironpython-users mailing list