Home > Article > Backend Development > Detailed introduction to Python’s object and type
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'sidentity 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.
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'stype 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 typeAs 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: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!