Python is an object-oriented language, and in Python everything is an object, a function is an object, and a type is an object.
Here's a look at the object-related content in Python.
Basic concepts of Python objects
There are three basic elements of a Python object:
- Identity: The object's unique identity flag, which is the memory address of the object (available with the built-in function ID ())
- type: the type of the object determines what type of value the object can hold, and what operations can be done (available with the built-in function type ())
- value: The data represented by the object
Here's a look at the following code:
Num =7PrintID (num)Printtype (num)PrintNumPrintdir (num)Print defIsEven (num):return(num%2 and[False]or[True]) [0]PrintID (isEven)Printtype (isEven)PrintIsEvenPrintDir (IsEven)
As you can see in the code, we define a numeric object of type int, and a function object, through the built-in Dir () function, we can view the properties/methods supported by the object:
Python Object origin
There are two most basic objects in Python, <type ' object ' > and <type ' type ', and these two objects are the origin of all objects.
Take a look at the relationship of <type ' object ' > and <type ' type ' > by the following code:
The code uses the "__class__" property to see the object's type, and the "__bases__" property to see the object's parent class, which is important to see the relationship between this object and other objects.
The following object graphs can be obtained based on the output of the above code:
In the Python object system, the relationship between <type ' object ' > and <type ' type ' > is like the relationship between chickens and eggs, and it cannot be said who preceded (created) who, the two are interdependent and together form the basis of the Python object system.
- The type of <type ' type ' > is <type ' type ' > (itself), <type ' type ' > parent class is <type ' object ' >
- <type ' object ' > Type is <type ' type ' >,<type ' object ' > No parent class
The introduction to here is not feeling a bit around, it doesn't matter, we continue to look at the following about the classification of objects.
Classification of Python objects
In Python, all objects can be divided into two categories: type Object, yes, type is also an object, and Non-type object (non-type objects).
The following is a concrete example of the classification of objects:
According to the output of the above example, we can get the following object diagram:
Here we will take list, mylist as an example to analyze:
- First, list is a python built-in container type , which means that list is a type Object
- As you can see from the "__class__" property, the type of the list object is <type ' type ' >
- As you can see from the "__bases__" property, the parent class of this type of object is <type ' object ' >
- You can see from the MyList "__class__" Property (<type ' list ' >) that the MyList is an object generated by the list type object (Non-type object (non-type))
From the above analysis we can verify the principles of Python object classification:
- If an object is an instance of <type ' type ' >, it is the type object, or Non-type object (non-type)
Type object and Non-type object
As you can see from the previous analysis, the type object, which includes:
- <type ' type ' >
- <type ' object ' >
- Object (Type Object) generated by <type ' type ' >
The type object has two important characteristics:
- can be quilt-like (subclassed)
- can be instantiated (instantiated)
Back to the object graph above, type object is the object that is stored in the first two squares.
Then the rest of the objects are Non-type object (non-type objects), back to the object graph, Non-type object (non-type object) is the one that is stored in the third square. The solid line cannot appear in the third square because the object cannot subclass (inherit), and the dashed arrow cannot appear in the third square because the object cannot be instantiated.
See <type ' type ' >
<type ' type ' > is itself a type object, and<type ' type ' > is the type of all types of objects (including <type ' type ' > itself) , that is, The type () or get "__class__" property for all types of objects will get <type ' type ' >.
We can import the types module and then use the Dir () built-in function to see all the built-in type objects, the types of which are <type ' type ';
See <type ' object ' >
Similarly, <type ' object ' > is a Type Object (because type (object) is <type ' type ' >), and<type ' object ' > is all type Object (except < The type ' object ' > itself) of the parent class .
Type () and __class__
What you need to mention here is that the type (), the built-in function, and the "__class__" property, both of which can be used to get the object types, generally the results are the same in both ways.
However, for the classic class in Python, the result of type () and "__class__" is different (this does not introduce the classic class and New-style Class):
The results for New-style Class,type () and "__class__" are the same.
Summarize
This article describes some of the basic points in Python objects:
- Three elements of a Python object
- Two basic objects in a Python object <type ' type ' > and <type ' object ' >
- Categories of Python objects: type Object (Type objects) and Non-type object (non-type objects)
With these points, you should have a basic understanding of Python objects. When you need to do object analysis, try to try the object graph used in the text, through this diagram, you can more intuitively see the relationship between the objects.
Python Object (top)