Functions required for Python to embed C/C ++ (lower-level embedding)

Source: Internet
Author: User

Embed Python into C/C ++ at a lower level, or if you want to get the values returned by a Python script, if you want to write a C program using related functions, you want to have a better understanding of how to embed Python into C/C ++ at a lower level, you can browse the following articles. Because Python has its own data type, you must use a special API to operate the corresponding data type in the C program. Common functions are as follows.

Number and string processing
The Python/c api provides the Py_BuildValue () function to convert numbers and strings to the corresponding data type in Python. The function prototype is as follows.

PyObject * Py_BuildValue (const char * format ,...)

The parameter meanings are as follows.

· Format: format the string, as shown in Table 8-1.

  • "IBM Forum 2010" held a grand smart theme
  • Win the data war with IBM's wisdom
  • Start with IBM's "Smart City"
  • Interview with CTO of boutong enterprise network division: upgrade to cloud computing
  • Introduction to the powerful functions generated when Python is embedded into C/C ++

The remaining parameters in the Py_BuildValue () function are integer, floating point, or string values in the C language to be converted. The returned value is a pointer of the PyObject type. In C, all Python types are declared as PyObject.

List operations

Python/c api provides the PyList_New () function to create a new Python list. The Return Value of the PyList_New () function is the list created. The function prototype is as follows.

PyObject * PyList_New (Py_ssize_t len)

The parameter meanings are as follows.

· Len: the length of the created list. After the list is created, you can use the PyList_SetItem () function to add items to the list. The function prototype is as follows.

 
 
  1. int PyList_SetItem( PyObject *list, Py_ssize_t index,
     PyObject *item) 

The parameter meanings are as follows.

· List: list of items to be added.

· Index: Location index of the added item.

· Item: the value of the added item.

You can also use the PyList_GetItem () function in Python/c api to obtain the value of an item in the list. The value returned by the PyList_GetItem () function. The function prototype is as follows.

PyObject * PyList_GetItem (PyObject * list, Py_ssize_t index)

The parameter meanings are as follows.

· List: list of operations to be performed.

· Index: index of the position of an item.

Python embedded in C/C ++) in the actual operation process, the Python/c api provides the function corresponding to the List Operation in Python. For example, the List append method corresponds to the PyList_Append () function. The sort method of the list corresponds to the PyList_Sort () function. The reverse Method of the list corresponds to the PyList_Reverse () function. The function prototype is as follows.

Int PyList_Append (PyObject * list, PyObject * item)

Int PyList_Sort (PyObject * list)

Int PyList_Reverse (PyObject * list)

For the PyList_Append () function, the parameter meanings are as follows.

· List: list of operations to be performed.

· Item: the item to participate in.

For PyList_Sort () and PyList_Reverse () functions, the parameter meanings are the same.

· List: list of operations to be performed.

The above is an introduction to Python embedded in C/C ++ at a lower level.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.