Details about type and inheritance relationship instances in Python

Source: Internet
Author: User

Details about type and inheritance relationship instances in Python

This article describes in detail the type and inheritance relationships in Python. Share it with you for your reference. The specific analysis is as follows:

If an object A holds the ID of another object B, then B can be retrieved after A is retrieved, and we say there is A navigation from A to B. This navigation relationship forms a complex network structure between all objects in Python.

Python programs include:

1. Modify the network structure;

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

(Side effects are devices outside the Python Virtual Machine. These codes are written in C or other languages. The code written in Python can only perform the first step of the function. Except print statements .)

Python objects have two navigation relationships: inheritance and type relationships, which are the most basic relationships in Python. The type relationship indicates the object through which an object is created. The inheritance relationship indicates the parent-child relationship between objects. This parent-child relationship plays a role in the name parsing process. Here I will first talk about the two relationships between the New Style classes. After understanding the relationships between the New Style classes, it is easy to explain the Classic classes.

The first thing to note is what type is in the built-in module. We all know that type can be used to determine the type of an object, as if it were a function. In fact, in 2.2, type is a class, which is not a common class. It is a class that can be created, called a metadata class. Run type (type) and print it. The type class is the core of the Python type system. The use of type as a function to determine the type is special, which may be caused by historical reasons. It may be more appropriate to use typeof.

How to construct a type

You must know that the class statement is used. However, in the core of Python, there is only one way to call the type constructor (because type is a type ). When running:

?

1

2

Class A (object ):

Def f (self): print 1

The Python parser will execute the following code:

?

1

2

3

Def f (self): print 1

A = type ('A', (object,),) # The parameter is (name, parent class tuple, member dict)

Del f

The two have almost the same effect. You can give it a try.

In addition to type, you can also use the _ class _ attribute to determine the type relationship. For example:

?

1

2

3

4

5

6

7

Class A (object): pass

A = ()

A. _ class _ # 'class _ main _.'

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, and the inheritance relationship forms a directed graph. All types are inherited from objects. "All" also includes type. The parent class of the object is still the object. Object also has its type as a type object, which is type. Therefore, the relationship between objects and types is like a chicken or an egg problem: type is inherited from the object (the inheritance relationship); object is generated by the type (the type relationship ). Use the issubclass or _ bases _ attribute to determine the inheritance relationship between two classes.

So what does inheritance from type mean? This means that the type of this class is type, and the parent class is also type. However, this method is meaningless in general programming (it is the core of meta programmming ). Generally, class statements are used instead of calling the type constructor to create type objects. To illustrate the syntax, let's take an example:

?

1

2

3

4

5

Class mytype (type): pass

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

Del f

A. _ class _ # class '_ main _. mytype'. The metadata is mytype.

Mytype. _ class _ # 'type'

When a class is defined, the type constructor is called indirectly. However, by setting the _ metaclass _ attribute, you can call the type subclass instead of the type. For example:

?

1

2

3

Class A (object ):

_ Metaclass _ = mytype

A. _ class _ # class '_ main _. mytype' is the same as the above method.

As a result, the type relationship of a Python object forms a tree structure, where type is at the root of the tree and is constructed by the type or type subclass, including class-defined classes (indirect call of type), class created by the constructor that calls type and type, system-defined types such as int list are in the middle node, and leaf nodes are instance objects. What is the type itself? Or type. This is the same as the root directory's parent directory or root directory.

The Classic class is different from the New Style class in that when a class is used to create a class, it does not indirectly call type, but indirectly call types. ClassType, and types. ClassType is created by type.

?

1

2

3

4

Class A: pass

Type (A) # type 'classobj '. Note that the _ class _ attribute does not exist.

Type (A) is types. ClassType # True

Types. ClassType. _ class _ # 'type'

I hope this article will help you with Python programming.

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.