How to better learn Python built-in objects

Source: Internet
Author: User

The ing between Python built-in objects and C struct is quite useful to us to some extent. Next we will look at how we can better use this advantage, and hope you will gain some benefits. The first thing to talk about is the Python built-in objects defined in C language. After the Python environment is initialized, these objects are created.

Now, the object and type abstraction of Python's built-in object-oriented mechanism have been mentioned. Next, let's take a look at how the actually existing objects in python are implemented in C language?

The following is a reference clip:

 
 
  1. PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in '
    type' */ PyAPI_DATA(PyTypeObject) PyBaseObject_Type; 
    /* built-in 'object' */  

The object is a basic object in Python. in C language, the corresponding struct is PyBaseObject_Type. From the name in C language, we can probably know that this class is a type object. In addition, the <type 'type'> In the Python built-in object corresponds to the PyType_Type in the C language.

The following is a reference clip:

 
 
  1. PyTypeObject PyType_Type = { PyObject_HEAD_INIT(&PyType
    _Type) 0, /* ob_size */ "type", /* tp_name */ sizeof
    (PyHeapTypeObject), /* tp_basicsize */ sizeof(PyMembe
    rDef), /* tp_itemsize */ …… };   

Let's take a look at the specific integer.

The struct of an integer instance in the C language is py1_bject. The following is a reference segment:

 
 
  1. typedef struct {   
  2. PyObject_HEAD   
  3. long ob_ival;   
  4. } PyIntObject;  

That is to say, through such a struct, we can point to the Python integer object in the C language runtime .. The corresponding Python Integer type object is:

 
 
  1. yTypeObject PyInt_Type = {   
  2. PyObject_HEAD_INIT(&PyType_Type)   
  3. 0,   
  4. "int",   
  5. sizeof(PyIntObject),   
  6. …… };  

Python built-in object custom object

When we create A Python object, it is all done through the underlying Python. After we define A class A by using the Python language, python first creates A class object such as A according to the code you write), and then when you need to create an instance of, in fact, the underlying layer of the Python built-in object is created through the Class Object.

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.