Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to explore the Python object system

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to explore the Python object system, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I. Python objects from a C perspective

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

When we think about problems, it may be easy to understand objects, but computers can only understand byte sequences such as the 0Magne1 sequence. Fundamentally speaking, the objects in our computer language are just 0Magne1 sequences in a piece of memory space. These consecutive or discontinuous memory spaces can be seen as a whole at a higher level. In Python, the general object we mentioned is a piece of memory space that the structure in C applies for on the heap Heap.

In order to implement the object-oriented mechanism of Python in C language, we need to define some structures that can manipulate the memory space of those objects.

1.PyObject&PyVarObject

All Python objects have something in common, which we highly abstract into a structure, PyObject.

Typedef struct _ object {PyObject_HEAD} PyObject; / / in fact, the macro PyObject_HEAD in the distribution version is int ob_refcnt; struct _ typeobject * ob_type

Ob_refcnt, which is object reference counting, exists to implement Python's reference-based garbage collection mechanism. There is also a pointer to a type object structure that represents the type of the object.

In the C language implementation, there is another structure extended to PyObject, that is PyVarObject, its content is PyObject_VAR_HEAD this macro, it has one more ob_size than PyObject, used to represent the length of variable length objects, see http://docs.python.org/c-api/structures.html for details.

Please don't be confused. There is no corresponding relationship between PyObject and PyVarObject and real objects in Python world. These two are just abstractions of all Python objects in C language representation. That is to say, in C language, as long as it is a Python object structure data, then the beginning of its memory will have several variables of the above structure body, so a PyObject pointer can point to all C language Python object structures, so that in the C language implementation, we can manipulate all built-in Python object structures through this unified pointer.

2.PyTypeObject

There is one thing that hasn't been mentioned just now, and that is the structure _ typeobject (PyTypeObject), which is an abstraction of all type objects in Python, so that we can call all type object structures through PyTypeObject pointers at the C language level.

Typedef struct _ typeobject {/ / Note the beginning is PyObject_VAR_HEAD PyObject_VAR_HEAD char * tp_name; / * For printing, in format "." * / int tp_basicsize, tp_itemsize; / * For allocation * / / * Methods to implement standard operations * / destructor tp_dealloc; printfunc tp_print;. / * More standard operations (here for binary compatibility) * / hashfunc tp_hash; ternaryfunc tp_call;. } PyTypeObject

Correspondence between 3.Python built-in object and C structure

Now that we have talked about the abstraction of objects and types in the object-oriented mechanism of Python, let's take a look at what the real objects in python look like when they are implemented in C language.

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

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

Object object is a relatively basic object in Python, and its corresponding structure in C language is PyBaseObject_Type. From this name in C language, we can roughly know that this class is a type object.

In addition, Python corresponds to PyType_Type in C language.

PyTypeObject PyType_Type = {PyObject_HEAD_INIT (& PyType_Type) 0, / * ob_size * / "type", / * tp_name * / sizeof (PyHeapTypeObject), / * tp_basicsize * / sizeof (PyMemberDef), / * tp_itemsize * /. }

Let's look at the more specific integers.

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

Typedef struct {PyObject_HEAD long ob_ival;} PyIntObject

That is to say, through such a structure, we can point to the integer object of Python in the runtime of C language.

Then the corresponding integer type object of our Python is

YTypeObject PyInt_Type = {PyObject_HEAD_INIT (& PyType_Type) 0, "int", sizeof (PyIntObject),. }

4. Custom object

When we create a Python object, we end up doing it through the bottom of Python. When we define our own class A through the Python language, Python first creates a class object like A (class object) according to the code you write, and then when you need to create an instance of A, it is actually created through the A Class object at the bottom of the Python.

II. Object system from the perspective of Python

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

Metaclasses,classes,instance

Classes can be divided into built-in type and user-defined class.

Let's use a picture to explain it in detail.

C is defined as follows (inheriting from a class in python is written directly in parentheses after the class name):

Class C (object):.

The solid line represents the relationship of is-kind-of and the dotted line represents the relationship of is-instance-of.

When viewing the base class of the current classes object (the instances object does not have a _ _ bases__ attribute), you can use classes_name.__bases__ to view it, and its value is a Tuple tuple (Python supports multiple inheritance).

The method to see the type of the current object is object_name.__class__

We can confirm the above figure through some tests:

Here, type is the class of all classes.

This is the end of the answer to the question on how to explore the Python object system. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report