List Info

Thread: Calling a Python function with parameters




Calling a Python function with parameters
country flaguser name
United Kingdom
2007-06-08 13:59:26
Hello,

I've now managed to load a python module using
boost::python::exec_file()

I can extract variables and run functions from C++ too.

But I am stumped on how to pass parameters from C++ to a
python function. 
Not much use without that!

I'm using
boost::python::extract<std::string>(global["pyFun
c"]) to run the 
function, where the python module may be:

##------------------------
def func1():
    return "hello from python"

pyFunc = func1()
##-----------------------------------


The docs don't seem to elaborate on how to add parameters.
There is plenty 
on calling C++ functions from python......

What I want to do is something like this:

// C++
answer =
extract<std::string>(global["pyFunc(""h
i"", ""there"" )"]

## python
def pyFunc( str1, str2 ):
    return str1+str2

Thanks for putting up with a newbie!

Simon



http://www.simonpickles.c
om  --- http://www.squirtualr
eality.com

____________________________________________________________
_____
Get a preview of Live Earth, the hottest event this summer -
only on MSN 
http://liveearth.msn.com?source=msntaglineliveearthhm

_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Calling a Python function with parameters
user name
2007-06-09 13:01:29
On 6/8/07, Simon Pickles <sipickleshotmail.com> wrote:
> Hello,
>
> I've now managed to load a python module using
boost::python::exec_file()
>
> I can extract variables and run functions from C++
too.
>
> But I am stumped on how to pass parameters from C++ to
a python function.
> Not much use without that!
>
> I'm using
boost::python::extract<std::string>(global["pyFun
c"]) to run the
> function, where the python module may be:
>
> ##------------------------
> def func1():
>     return "hello from python"
>
> pyFunc = func1()
> ##-----------------------------------
>
>
> The docs don't seem to elaborate on how to add
parameters. There is plenty
> on calling C++ functions from python......
>
> What I want to do is something like this:
>
> // C++
> answer =
extract<std::string>(global["pyFunc(""h
i"", ""there"" )"]
>
> ## python
> def pyFunc( str1, str2 ):
>     return str1+str2
>
> Thanks for putting up with a newbie!

I guess this is what your are looking for:
http://
boost.org/libs/python/doc/v2/call.html

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-
binding.net/
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )