Getting argument list in a Boost:Python function -


in cpython can argument list of function following methods. function name 'amethod'

import inspect inspect.getargspec(amethod) 

or

amethod.func_code.co_varnames 

how can achieve same thing boost:python function? following error when use these methods in those.

for first method typeerror: not python function

for second method attributeerror: 'amethod' object has no attribute 'func_code'

when accessing python attribute on boost::python::object, use attr member function. example:

amethod.func_code.co_varnames 

would become

amethod.attr("func_code").attr("co_varnames") 

here complete example.

#include <iostream> #include <vector>  #include <boost/foreach.hpp> #include <boost/python.hpp> #include <boost/python/stl_iterator.hpp>  void print_varnames(boost::python::object fn) {   namespace python = boost::python;   typedef python::stl_input_iterator<std::string> iterator;    std::vector<std::string> var_names(     iterator(fn.attr("func_code").attr("co_varnames")),     iterator());    boost_foreach(const std::string& varname, var_names)     std::cout << varname << std::endl; }  boost_python_module(example) {   def("print_varnames", &print_varnames); } 

usage:

>>> def test1(a,b,c): pass ...  >>> def test2(spam, eggs): pass ...  >>> def test3(): pass ...  >>> example import print_varnames >>> print_varnames(test1) b c >>> print_varnames(test2) spam eggs >>> print_varnames(test3) >>>  

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -