Version Python 2.6
IDE Microsoft Visual Studio 2008
C + + calls Python script
1.
Py_initialize () Initializes Python, both of which correspond.
Py_finalize () Clean up python
2. Import Python module
Pyobject *pmodule = Pyimport_importmodule ("Testhello");
Note: Do not Add. py, VC environment do not use test file name.
3. Get the functions within the Python script
Pyobject *pfunc = pyobject_getattrstring (Pmodule, "Hello");
4. Invoke Python function with no parameters
Pyeval_callobject (Pfunc, NULL);
5. Call python function, pass parameter
Pyobject *pargs = Py_buildvalue ("(s)", "Thank all the same");
Note: The format passed must be a tuple tuple. Which is to add (). Otherwise, the script is abnormal.
6. Process return value
Pyeval_callobject is returned after the call.
Pyobject *presult = Pyeval_callobject (Pfunc, PArgs);
Returns an integral type, using &.
int i = 0;
Pyarg_parse (PResult, "I", &i);
Returns a floating-point type
float d = 0.1f;
Pyarg_parse (PResult, "F", &d);
Returns a string
char* s = "";
Pyarg_parse (PResult, "s", &s);
Note: It is possible to write &s as s directly in C + +.
But when interacting with Python 2.6, write &s, a pointer to a pointer. Otherwise it will
Generates an exception.
7. Each pyobject* remembers to destroy with Py_decref when it is not needed.
8. Return value is tuple tuple data
The return value is list data
The return value is dict dictionary data
Pytuple_new New META Group
Pytuple_size Get tuple size
Pytuple_getitem get a single element
Pytuple_setitem Inserts a new element
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service