The secret of Python object system

Source: Internet
Author: User

Guido created Python in C, and everything in the Python world was an object.

I. Python objects in the C perspective

Let's go back to the source, Python is implemented in C, and provides a C API http://docs.python.org/c-api/index.html to the outside.

When we think about the problem, it may be easy to understand the object, and the computer can understand only the 0,1 sequence of such a sequence of bytes, fundamentally speaking, the computer language of the object is only in memory of a memory space in the 0,1 sequence, These contiguous or discontinuous memory spaces can be considered as a whole at a higher level. In Python, the general object we refer to is the block of memory that the struct in C requests on the heap.

In order to be able to implement Python's object-oriented mechanism in C, we need to define some structures to manipulate the memory space of those objects.

1.pyobject&pyvarobject

All Python objects have something in common, and we abstract it highly into a structural body pyobject

C code
    1. typedef struct _object{
    2. Pyobject_head
    3. } Pyobject;
    4. In fact pyobject_head this macro in the release version of the
    5. int ob_refcnt;
    6. struct _typeobject *ob_type;

OB_REFCNT, which is the object reference count, exists to implement the Python-based garbage collection mechanism of the reference technology.

There is also a pointer to the struct of a type object that represents the type of the object.

In the implementation of the C language, there is also a structure extended to Pyobject

That is pyvarobject, its content is pyobject_var_head this macro, it is more than pyobject a ob_size, used to denote the length of the variable length of the object, details see http://docs.python.org/c-api/ Structures.html

And let's not get confused, there is no correspondence between the Pyobject and the Pyvarobject and the real objects in the Python world, both of which are just an abstraction of the Python object in the C language representation. That is, in C language, As long as the data of the structure of a Python object, the beginning of its memory will have a few variables of the structure above, so a pyobject pointer can point to all the C language of the structure representing the Python object, so in the implementation of C language, We can manipulate all the built-in Python object structures with this unified pointer.

2.PyTypeObject

There is just one more thing that is not said, that is _typeobject (pytypeobject) This struct, which is the abstraction of all types of objects in Python, So we can use the Pytypeobject pointer to all types of object structures in the C language hierarchy.

C code
  1. typedef struct _typeobject {
  2. Note that the start section 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
  12. Binary compatibility) */
  13. Hashfunc Tp_hash;
  14. Ternaryfunc Tp_call;
  15. ......
  16. } Pytypeobject;

3.Python built-in objects and C-structure correspondence

Now that the objects and types of Python object-oriented mechanisms have been abstracted, let's take a look at what happens when the real object in Python is implemented in the C language.

The first thing to talk about is the python built-in objects, which are defined by the C language, which are created when the Python environment is initialized.

C code
    1. Pyapi_data (Pytypeobject) Pytype_type; / * built-in ' type ' * /
    2. Pyapi_data (Pytypeobject) Pybaseobject_type; / * built-in ' object ' * /

Object objects in Python is a relatively basic object, its corresponding structure in C language is Pybaseobject_type, from the C language of this name we can probably know that this class is a type object.

There is <type ' type ' in Python > corresponds to Pytype_type in C language

C code
    1. Pytypeobject Pytype_type = {
    2. Pyobject_head_init (&pytype_type)
    3. 0,/ * ob_size * /
    4. "Type",/ * Tp_name * /
    5. sizeof (Pyheaptypeobject),/ * tp_basicsize * /
    6. sizeof (PYMEMBERDEF),/ * tp_itemsize * /
    7. ......
    8. };

Let's look at more specific integers.

The structure of an integer instance representation in the C language is Pyintobject

C code
    1. typedef struct {
    2. Pyobject_head
    3. Long Ob_ival;
    4. } Pyintobject;

This means that we can point to the Python integer object in the C language's runtime.

Then the corresponding US Python integer type object is

C code
    1. Ytypeobject Pyint_type = {
    2. Pyobject_head_init (&pytype_type)
    3. 0,
    4. "Int",
    5. sizeof (Pyintobject),
    6. ......
    7. };

4. Custom Objects

When we create a Python object, we end up doing it through the bottom of Python,

After we have defined our own class A in Python, Python begins by creating a class object (class object) based on the code you write, and then when you need to create an instance of a, In fact, the bottom of Python is created through a class object.

Two. Object architecture from the Python perspective

In the pure Python world, everything is an object. These objects can be divided into three categories,

Metaclasses,classes,instance

Where classes can be divided into built-in type and user-defined class

Let's go through a picture for a detailed explanation.


Note:

where c is defined in the following way (Python inherits from a class that is written directly in parentheses following the class name):

Python code
    1. Class C (object):
    2. ......


Where solid lines represent is-kind-of relationships, and dashed lines represent is-instance-of relationships.

When viewing the base class for the current classes object (the Instances object does not have a __bases__ property), you can view it with classes_name.__bases__, which is a tuple tuple (Python supports multiple inheritance).

The way to view the type of the current object is object_name.__class__

We can use some tests to confirm the above diagram:


Type is a class of all classes

Postscript:

After reading the relevant chapters of "Python source code", I feel that the author is not very organized or that his thinking is not in keeping with the author's thinking. So I would like to take a free time to follow my own way of thinking to organize.

The language of your own computer is C, so it's handy to write.

The secret of Python object system

Related Article

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.