How to Implement Python in C

Source: Internet
Author: User

The Python language appears in many languages. There are many problems in our continuous learning and usage. Next we will learn the relevant knowledge and how to use the C language environment in detail. I hope you will have some gains.

  • Exploring a variety of Python languages
  • Describes various modules in Python
  • Interpreting the processing file in Python
  • Introduction to the Python language
  • Analysis of simple and convenient Python language

When we think about the problem, it may be easy to understand the object, but the computer can only understand the byte sequence such as the 0, 1 series. Basically, the objects in our computer language are only 0, 1 sequences in a memory space in the memory. These continuous or non-continuous memory spaces can be considered as a whole at a higher level. in Python, the general objects we mentioned are a piece of memory space applied by the structure in C on Heap.

In order to implement the object-oriented mechanism of Python in C language, some structures need to be defined to operate the memory space of those objects.

All Python objects share something in common. We can abstract them into a structure PyObject.

 
 
  1. Typedef struct _ object {
  2. PyObject_HEAD
  3. } PyObject;
  4. // In fact, the macro PyObject_HEAD is
  5. Int ob_refcnt;
  6. Struct _ typeobject * ob_type;
  7. Typedef struct _ object {
  8. PyObject_HEAD
  9. } PyObject;
  10. // In fact, the macro PyObject_HEAD is
  11. Int ob_refcnt;
  12. Struct _ typeobject * ob_type;

Ob_refcnt is the object reference count. It exists to implement the reference-based garbage collection mechanism in Python.

Another is a pointer to a struct of the type object to represent the type of the object.

In the C language implementation, there is also a struct extended to PyObject

That is, PyVarObject, whose content is the macro PyObject_VAR_HEAD, which has an ob_size more than PyObject,

Another point is to avoid obfuscation. Here, PyObject and PyVarObject have no correspondence with real objects in the Python world. These two are only an abstraction of all Python objects in the C language representation. that is to say, in C language, as long as it is the data of a Python object structure, the initial part of its memory will have several variables of the above structure, therefore, a pointer to a PyObject can point to all structures in C that represent Python objects. In this way, in the implementation of C, we can use this unified pointer to operate all the built-in Python object structs.

Another thing I just didn't talk about is the structure of _ typeobject (PyTypeObject), which is the abstraction of all types of objects in Python, in this way, we can use the PyTypeObject pointer to call all types of object structures at the C language level.

C code

 

 
 
  1. Typedef struct _ typeobject {
  2. // Note that the start part is PyObject_VAR_HEAD.
  3. PyObject_VAR_HEAD
  4. Char * tp_name;/* For printing, in format
  5. "<Module>. <name> "*/
  6. Int tp_basicsize, tp_itemsize;/* For allocation */
  7. /* Methods to implement standard operations */
  8. Destructor tp_dealloc;
  9. Printfunc tp_print;
  10. ......
  11. /* More standard operations (here
  12. Binary compatibility )*/
  13. Hashfunc tp_hash;
  14. Ternaryfunc tp_call;
  15. ......
  16. } PyTypeObject;

The above is the usage of the Python language in the C language environment.

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.