How to build PythonTuple from C #

I want to build an IronPython tuple from C #. These are the public PythonTuple constructors:

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

How could I build, for example, a tuple (1, 2, 3)?

+3
source share
3 answers

In fact, you can give the constructor object any enumerable object. It can be a string ArrayList, a List<object>, a List<string>, a PythonDictionary, a HashSet, string, an array of bytes. Whatever you want - if you can list it in IronPython, you can pass it to the constructor.

So, for example, you could do:

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

I found the answer on the mailing list somewhere:

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

- PythonTuple(object) 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);
}
+1

Source: https://habr.com/ru/post/1718347/


All Articles