Examples of type relationships and inheritance relationships in Python

Source: Internet
Author: User
Tags constructor inheritance parent directory in python

This article describes in detail the types and inheritance relationships in Python. Share to everyone for your reference. The specific analysis is as follows:

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

The running of Python programs includes:

1. Modify the network structure;

2. Execute a side-effect code object (or bytecode, see Python Language Reference 3.2)

(side effects are devices that affect the Python virtual machine, which are written in C or another language, and the code written in Python only completes the first step.) Except for print statements. )

There are two navigation relationships between Python objects: inheritance relationships and type relationships, which are the most basic relationships in Python. A type relationship describes which object the object is created from, and the parent-child relationship between the object, which plays a role in the parsing of the name. Here I start by saying that the two relationships between the new style class, mastering the relationship of the new style class, will explain the classic class easily.

The first thing to explain 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, type is a class in 2.2 and is not a normal class, it is a class that can create a class, called a meta class. You run type (type) and try it, print it. The type class is the core of the Python type system. Using type as a function of the judgment type is a special case, perhaps due to historical reasons, perhaps more appropriate with TypeOf.

How to construct a type

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

?

1 2 Class A (object): Def f (self): print 1

The Python parser performs the functions shown in the following code:

?

1 2 3 def f (self): print 1 a=type (' A ', (object,),) # parameter is (first name, parent class tuple, member Dict) del F

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

The determination of a type relationship can also use the __class__ property, in addition to using type. Such as:

?

1 2 3 4 5 6 7 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.__clas S__ # True

Inheritance relationship

Inheritance relationships only occur between types, and inheritance relationships constitute a forward graph. All types are inherited from object. "All" Of course also includes type. The parent class of object or object. Object also has its type as a type, which is type. So the relationship between object and type is like having a chicken or an egg first: type is inherited from object (inheritance relationship); object is generated by type (type relationship). The inheritance relationship between two classes is judged by Issubclass or __bases__ properties.

So what does it mean to inherit from type? That means the class is type, and the parent class is also type. But this approach is meaningless in general programming (at the heart of the Meta programmming). Because you typically use class statements rather than by calling the type's constructor, you create a type object. To illustrate the syntax, give an example:

?

1 2 3 4 5 Class MyType (type): Pass A=mytype (' A ', (object,), {}) # del F a.__class__ # class ' __main__.mytype ', the Meta class is MyType MYTYPE.__CLA ss__ # ' type '

When a class is defined with class, the constructor of type is called indirectly. However, by setting the __metaclass__ property, you can not call type, but instead call the subclass of type. Such as:

?

1 2 3 Class A (object): __metaclass__ = mytype a.__class__ # class ' __main__.mytype ', as shown in the above way.

Thus, the type relationship of the Python object consists of a tree structure, where type is at the root of the tree, the type constructed by the type or subclass of type, including class-defined classes (indirect call type), class created by calling the type, subclass constructor of type, int The system definition type such as list is in the middle node and the leaf node is the instance object. What is the type itself? This is the same as the parent directory of the root directory or the root directory.

The classic class differs from the new style class in that when you create a class with class, instead of indirectly calling the type, you call types indirectly. ClassType, and types. The ClassType is created by type.

?

1 2 3 4 Class A:pass Type (A) # type ' classobj ', note that there is no __class__ property. Type (A) is types. ClassType # True types. classtype.__class__ # ' type '

I hope this article will help you with your Python programming.

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.