In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you the parsing of how to deal with Python object parameters. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
The extension module written in C language in the Python object must be compiled into the form of dynamic link library. The function PyArg_ParseTuple () provided by Python's C language extension interface is usually used to obtain these parameter values. I hope this article can be helpful to you.
Python is an object-oriented scripting language, and all objects are represented in the Python interpreter as PyObject,PyObject structures that contain all member pointers of Python objects. And the type information and reference count of Python objects are maintained. When programming Python extensions, once the Python object is processed in C or C++, it means maintaining a PyObject structure.
In the C language extension interface of Python, most functions have one or more parameters of PyObject pointer type, and most of the return values are PyObject pointers. To simplify memory management, Python implements automatic garbage collection through the reference counting mechanism, and each object in Python has a reference count.
Used to count how many times the object has been referenced in different places. Every time a Python object is referenced, the corresponding reference count increases by 1, and each time the Python object is destroyed, the corresponding reference is subtracted by 1. It is only when the reference count is 00:00 that the Python object is actually deleted from memory.
The following example shows how the Python interpreter uses reference counting to manage Pyhon objects:
# include PyObject* wrap_fact (PyObject* self, PyObject* args) {int n, result; if (! PyArg_ParseTuple (args, "i:fact", & n) return NULL; result = fact (n); return Py_BuildValue ("I", result);} static PyMethodDef exampleMethods [] = {{"fact", wrap_fact, METH_VARARGS, "Caculate N!"}, {NULL, NULL}}; void initexample () {PyObject* m; m = Py_InitModule ("example", exampleMethods);}
Correct maintenance of reference count is a key issue when dealing with Python objects in Cpicabet +, and memory leaks can easily occur if it is not handled properly. Python's C language interface provides macros to maintain reference counting, the most common of which is to use Py_INCREF () to increase the reference count of Python objects by 1, and Py_DECREF () to subtract the reference count of Python objects by 1.
This function is the interface between the Python interpreter and the C function, with two parameters: self and args. The parameter self is used only when the C function is implemented as an inline method (built-in method). Usually the value of this parameter is NULL, and the parameter args contains all the parameters to be passed to the C function by the Python interpreter. These parameter values are usually obtained by using the function PyArg_ParseTuple () provided by the C language extension interface of Python.
Each item in the method list consists of four parts: the method name, the derived function, the way parameters are passed, and the method description. The method name is the name under which the method is called from the Python interpreter. The mode of parameter transfer specifies the specific form of passing parameters from Python to C function, and the two optional ways are METH_VARARGS and METH_KEYWORDS.
METH_VARARGS is the standard form of parameter transfer, which passes parameters between the Python interpreter and the C function through the tuple of Python. If the METH_KEYWORD mode is adopted, the parameters between the Python interpreter and the C function will be passed between them through the dictionary type of Python.
The above is the parsing of how to deal with Python object parameters shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.