How to parse Python object parameters

Source: Internet
Author: User

The extension module written in C language must be compiled into a dynamic link library in a Python object. The PyArg_ParseTuple () function provided by the Python C Language extension interface is usually used () to obtain these parameter values. I hope this article will help you.

Python is a scripting language implemented in C language. It has excellent openness and scalability and provides convenient and flexible application interfaces (APIS ). This allows C/C ++ programmers to expand the functions of the Python interpreter at various levels. Before using C/C ++ to Expand functions of Python, you must first understand the C language interface provided by the Python interpretation.

Python is an object-oriented scripting language. All objects are expressed as PyObject In the Python interpreter. The PyObject structure contains all member pointers of Python objects. Maintain the type information and reference count of Python objects. During Python extension programming, once Python objects must be processed in C or C ++, A PyObject structure should be maintained.

In Python C Language extension interfaces, most functions have one or more parameters of the PyObject pointer type, and most of the returned values are PyObject pointers. To simplify memory management, Python uses the reference counting mechanism to implement automatic garbage collection. Each object in Python has a reference count.

It is used to count the number of times the object is referenced in different places. Every time a Python object is referenced, the corresponding reference count increases by 1. Every time a Python object is destroyed, the corresponding reference is reduced by 1. Only when the reference count is zero, to delete Python objects from the memory.

The following example illustrates how the Python interpreter manages Pyhon objects by using reference counts:

 
 
  1. #include <Python.h> 
  2.  
  3. PyObject* wrap_fact(PyObject* self, PyObject* args)   
  4. {  
  5.   int n, result;  
  6.     
  7.   if (! PyArg_ParseTuple(args, "i:fact", &n))  
  8.     return NULL;  
  9.   result = fact(n);  
  10.   return Py_BuildValue("i", result);  
  11. }  
  12.  
  13. static PyMethodDef exampleMethods[] =   
  14. {  
  15.   {"fact", wrap_fact, METH_VARARGS, "Caculate N!"},  
  16.   {NULL, NULL}  
  17. };  
  18.  
  19. void initexample()   
  20. {  
  21.   PyObject* m;  
  22.   m = Py_InitModule("example", exampleMethods);  

When processing Python objects in C/C ++, correct maintenance of the reference count is a key issue. If it is not handled properly, memory leakage may occur. Python's C language interface provides some macros to maintain the reference count. The most common is to use Py_INCREF () to increase the reference count of Python objects by 1, and use Py_DECREF () to reduce the reference count of Python objects by 1.

This function is an interface between the Python interpreter and the C function. It has two parameters: self and args. The self parameter is used only when the C function is implemented as an inline method (built-in method. Generally, the value of this parameter is NULL. The args parameter contains all the parameters that the Python interpreter will pass to the C function, the PyArg_ParseTuple () function provided by the Python C Language extension interface is usually used to obtain these parameter values.

Each item in the method list consists of four parts: method name, export function, parameter transfer method, and method description. The method name is the name used to call this method from the Python interpreter. The parameter passing method specifies the specific form of passing parameters from Python to the C function. The two optional methods are METH_VARARGS and METH_KEYWORDS.

METH_VARARGS is the standard form of parameter passing. It uses the Python tuples to pass parameters between the Python interpreter and the C function. If METH_KEYWORD is used, then, the Python interpreter and the C function will pass parameters between the two through the Python dictionary type.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.