tags:

views:

165

answers:

3

I want to construct an IronPython tuple from C#. These are PythonTuple's public constructors:

    public PythonTuple();
    public PythonTuple(object o);

How would I construct, for example, the tuple (1, 2, 3)?

A: 

I found the answer on a mailing list somewhere:

PythonTuple myTuple = PythonOps.MakeTuple(new object[] { 1, 2, 3 });
dangph
+1  A: 

One way of doing this is to use the PythonTuple(object) constructor with an IronPython.Runtime.List:

// IronPython.Runtime.List
List list = new List();
list.Add(1);
list.Add(2);
list.Add(3);

PythonTuple tuple = new PythonTuple(list);

foreach (int i in tuple)
{
    Console.WriteLine("Tuple item: {0}", i);
}
bobbymcr
+1  A: 

You can actually give the object constructor any enumerable object. This could be an ArrayList, a List, a List, a PythonDictionary, a HashSet, a string, a byte array. Whatever you want - if you can enumerate it in IronPython then you can give it to the constructor.

So for example you could do:

new PythonTuple(new[] { 1, 2, 3 });

Dino Viehland