Examples of type relationships and inheritance relationships in Python

Source: Internet
Author: User
This article details the type relationships 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 after retrieving a we can retrieve B, we say there is a to B navigation. This navigational relationship creates a complex network structure between all the objects in Python.

The operation of the Python program includes:

1. Modify the network structure;
2. Execute a code object with side effects (bytecode, see Python Language Reference 3.2)
(Side-effects are those that affect devices outside the Python virtual machine, which are written in C or other languages, and Python-written code can only do the first step.) Except for the print statement. )

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

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

How to construct a type

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

Class A (object):  def f (self): print 1

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

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

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

Type relationship determination you can also use the __class__ property in addition to the type. Such as:

Class A (object): Passa=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

An inheritance relationship occurs only between types, and an inheritance relationship forms 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 egg first: type is inherited from object (inheritance relationship); object is generated by type (kind relationship). The inheritance relationship between two classes is judged by the Issubclass or __bases__ property.

So what does it mean to inherit from type? That means the class is of type, and the parent class is also the type. But this approach is meaningless in general programming (but it is the core of Meta programmming). Because you typically use a class statement, instead of creating a type object by calling the constructor of type. To illustrate the syntax or to cite an example:

Class MyType (Type): Passa=mytype (' A ', (object,), {}) # del fa.__class__ # class ' __main__.mytype ', meta-class mytypemytype.__ class__ # ' type '

When you define a class with class, the constructor of type is called indirectly. However, by setting the __metaclass__ property, you can call a subclass of type without calling the type. Such as:

Class A (object): __metaclass__ = mytypea.__class__ # class ' __main__.mytype ', as shown in the above method.

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

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

Class A:passtype (A) # type ' classobj ', note that there is no __class__ attribute. Type (A) is types. ClassType # truetypes.classtype.__class__ # ' type '

Hopefully this article will help you with 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.