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)?

3 Answers 3

6

You can actually give the object constructor any enumerable object. This could be an ArrayList, a List<object>, a List<string>, 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 });
Sign up to request clarification or add additional context in comments.

Comments

2

I found the answer on a mailing list somewhere:

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

Comments

1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.