[C++-sig] Calling function on object of unknown type

David Roy david.roy.perso at gmail.com
Mon Jul 20 16:32:34 CEST 2009


Hi all,

I'm extending C++ with Boost.Python.
At some point I need to call a Python function on an object of unknown
Python type.
I found those 2 solutions, but they seem so simple that they make me wonder
about the dangerosity of doing this (I'm a newbie to Boost.Python):

static void test_func_attr(object pyObj, object arg)
{
	pyObj.attr("test")(arg);		
}

BOOST_PYTHON_MODULE(my_module)
{
	def("test_func_attr", &test_func_attr);	
}

Called from :

class MyClass(object):
     def test(self,str):
        print str

class = MyClass()
test_func_attr(class, "hello")

The second solution is very close:

static void test_func_attr(object pyObj, object arg)
{
    object func = pyObj.attr("test");
    call<void>(extract<PyObject*>(func), arg);		
}

Both are working. Am I doing something wrong here? And which one is the best
(advantage of #1 is that I don't need to know the return type of test())

Thanks in advance
-David 
-- 
View this message in context: http://www.nabble.com/Calling-function-on-object-of-unknown-type-tp24570986p24570986.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.



More information about the Cplusplus-sig mailing list