MAG
MAG

Reputation: 3075

PyTuple_Pack which takes in variable number of arguments

I am on Python 2.6.6 & calling python from c++ .

How can i write PyTuple_Pack which takes in variable number of arguments .

I have variable arguments coming which I use a for loop and populate my array . My array has max of 1000 elements but i populate only top no_of_argument elements using

python_obj[i] = PyString_FromString(arg[i])

I DO NOT want to write code like

if(no_of arg ==1 )
    return  PyTuple_Pack(1, python_obj[0]);
if(no_of arg ==2 )
    return  PyTuple_Pack(2, python_obj[0],python_obj[1]);

I want something like return PyTuple_Pack(no_of_args, followed by my array where pyobjects exist ;

On net i can see http://docs.python.org/release/2.4.4/whatsnew/node14.html

A new function, PyTuple_Pack(N, obj1, obj2, ..., objN), constructs tuples from a variable length argument list of Python objects. (Contributed by Raymond Hettinger.)

However example to write good code using above is not found by my search for which i need your help pls..

Upvotes: 0

Views: 1896

Answers (1)

user2357112
user2357112

Reputation: 281624

Use PyTuple_New to make a tuple with the right size, then iterate over your array and use PyTuple_SetItem to fill the tuple with values.

Upvotes: 1

Related Questions