Type relationship and inheritance relationship in Python

xiaoxiao2021-03-06  58

Everything in Python is an object, and each object has a unique unstable ID (inquired by ID function). Associate a name and an ID, you can retrieve this object via this name. If an object A holds another object B, you can retrieve B after retrieving A, we say there is a navigation of A to B. This navigation relationship enables a complex network structure between all objects in Python.

The operation of the Python program includes:

1. Modify this network structure;

2. Execute the code object with side effects (Code Object or bytecode, see Python Language Reference 3.2)

(Side effect refers to devices that affect the Python virtual machine. These codes are written in c or other language. Python written by Python can only complete the first step. Theprint statement except.)

There are two navigation relationships between Python objects: inheritance relationship and type relationship, is the most basic relationship in Python. Type Relation Description A object is created by which object is created; inheritance of the father and child relationship between the object, this parent-child relationship plays a role in the parsing process of the name. Here I first said the two relationships between the New Style class, after mastering the relationship between the New Style class, then explain that the Classic class is easy.

First, you need to explain what is TYPE in the built-in module. Everyone knows that Type can be used to determine the type of object, it seems to be a function. In fact, Type in 2.2 is a class, and is not a normal class, is a class that can create classes called a category. You run Type (Type), printing

. The Type class is the core of the Python type system. Use Type as a function of a function of judgment is a more special case, perhaps, by historical reasons, and be more appropriate to use TypeOf.

How to construct a type

You must know that it is using a Class statement. But in fact, in the heart of Python, there is only one way, that is, call Type's constructor (because Type is a type). When running:

Class A (Object):

DEF F (Self): Print 1

The Python parser will perform the following code:

DEF F (Self): Print 1

A = Type ('a', (object,) # parameters (name, parent Tuple, member Dict)

Del f

The effect is almost the same, you can try it.

Determination of Type Relations In addition to using TYPE, the __class__ attribute can be used. Such as:

Class A (Object): Pass

A = a ()

a .__ class__ # 'class __main __. a'

A .__ class__ # 'Type

'

TYPE .__ Class__ # 'Type

'

TYPE .__ class __.__ class__ # 'Type

'

Type .__ class__ is type .__ class .__ class__ # TRUE

Inheritance relationship

The inheritance relationship only occurs between types, inheritance relationships constitute a directional view. All types are inherited from Object. "All" of course also include Type. The parent class of Object or Object. Object as a type object is also its type, this type is TYPE. Therefore, the relationship between Object and Type is like a problem with the first chicken or the first egg: TYPE is inherited from Object (inheritance); Object is generated by Type (type relationship). Judging the inheritance relationship between the two classes through the Issubclass or __bases__ attribute. So what does it mean from Type? That means this type of type is Type, and the parent class is also Type. But this approach is not meaningful in general programming (but the core of meta programmming). Because of the general use of the Class state, the type object is created instead of calling the Type constructor. For explanation of grammar or an example:

Class mytype (Type): Pass

A = mytype ('a', (object,), {}) #

Del F

A .__ class__ # class '__main __. Mytype', class class is mytype

mytype .__ class__ # 'Type

'

When defining a class with CLASS, the TYPE constructor is indirectly called. But by setting the __metaclass__ attribute, you may not call Type, but call the subclass of TYPE. Such as:

Class A (Object):

__metaclass__ = mytype

A .__ class__ # class '__main __. Mytype', and the same as the above way.

As a result, the type of Python object consists of a tree structure, where Type is in the root of the tree, the type of TYPE or TYPE subclass, including class defined classes (indirect call type), call Type, Type Class constructor created class, int LIST, etc. System definition type is in the intermediate node, the leaf node is the Instance object. What is the type of Type itself? Or Type. This is the same as the parent directory of the root directory or the root directory.

The Classic class is different from the New Style class. When you create a class with CLASS, it is not indirectly calling Type, but indirect call types.classtype, and Types.ClasStype is created by Type.

Class A: Pass

TYPE (a) # type 'classobj', notice that there is no __class__ attribute.

Type (a) is types.classtype # TRUE

Types.classtype .__ class__ # 'type'

转载请注明原文地址:https://www.9cbs.com/read-118060.html

New Post(0)