Home  >  Article  >  Backend Development  >  Detailed introduction to Python’s object and type

Detailed introduction to Python’s object and type

高洛峰
高洛峰Original
2017-03-20 11:25:311887browse

1. Excerpt from Python Documentation 3.5.2 Partial explanation

Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann's model of a “stored program computer,” code is also represented by objects.)

Object is Python’s abstraction of data. All data in a Python program is represented by objects or relationships between objects. (In a sense, and consistent with von Neumann's "stored program computer" model, code is also represented by objects).

Every object has an identity, a type and a value. An object's

identity never changes once it has been created; you may think of it as the object's address in memory. The 'is' operator compares the identity of two objects; the id() function returns an integer representing its identity.

Each object has An identity, a type and a value. The identity of an object does not change once it is created; you can think of it as the address of the object in memory. 'is'

OperatorCompares the identities of two objects; id()FunctionReturns an integer representing their identity.

An object's types determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values ​​for objects of that type. The type() function returns an object's type ( which is an object itself). Like its identity, an object's

type is also unchangeable.

The type of an object determines the operations the object supports (for example, "Does it have a length?"), And also defines the possible values ​​for objects of this type. The type() function returns the type of an object (which is the object itself). Like its identity, an object's type is immutable.

2. Pyhtml explanation:

object:

class object
      The most base type

type:

class type(object)
      type(object_or_name, bases, dict)
      type(object) -> the object's type
      type(name, bases, dict) -> a new type

Detailed introduction to Python’s object and type

As can be seen from the above three figures, object object is the most basic type, which is a comprehensive abstract concept of data. Compared with the object object, the type type is a slightly more specific abstract concept. It is said to be specific because it already has the factors to refine the more specific abstract concept from the object object. This is why type(int), type(

float), type(str), type(list), type(tuple), type(set), etc. are all types, which is why The reason why instance(type, object) and instance(object, type) are both True is that the type type is the overall concept of types such as int and float. So, why issubclass(type, object) is True, and issubclass(object, type) is False? From the second picture, that is, from the inheritance relationship, we can see that type is a subclass of object, so the former is True and the latter is False. From the perspective of the overall design of the Python language, there are objects first and then relatively specific types, that is, the design idea of ​​giving priority to the whole over the parts.

If you look at these problems from a more essential perspective, you should find the reasons from Python Documentation-->3. Data Model-->3.1 Objects, values ​​and types [Please refer to Python official standards Library], you can see from the standard library:

  • object is Python’s abstraction of data, and it is the concentrated expression of data by Python programs.

  • Every object has an identity, a type and a value.

  • The type of object determines the operations supported by the object.

  • The values ​​of some objects can be changed. Objects whose values ​​can be changed are called mutable objects; objects whose values ​​cannot be changed after creation are called immutable objects.

Therefore, from the perspective of the overall design system of Python, there are objects first, then identifiers, types and values, followed by operations on objects, etc. This also explains the meaning of Figure 3 The reason for the result.

The above is the detailed content of Detailed introduction to Python’s object and type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn