In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "Python everything object source code analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python everything object source code analysis" bar!
Everything is an object.
The content of this blog is mainly aimed at the understanding of everything in Python, and sort out the type and object system of Python as a whole.
In Python, everything is an object, an integer is an object, a string is also an object, and a basic type (such as int) is also an object. Python no longer discriminates between basic types and objects, and all basic types are implemented internally by objects.
> a = int > b = 1 > id (a) 140734789683952 > id (int) 140734789683952 > a > id (b) 2421963817200 > > id (1) 2421963817200 > > b11 type objects and instance objects
A type in Python is an object called a type object. The integer type, the string type, and the custom type we defined through the class keyword are also an object.
Through class instantiation, you can get an instantiated object, called an instance object.
2 types, object system 2. 1 meta-types type
We mentioned earlier that a type in Python is a kind of object called a type object. So what is the type of type object?
> > type (int) > int.__class__
As you can see, the type of a type is type, which we call a meta-type, but this type is special, and its instance object is a type object. In addition, there is a special type in Python, object, and all other types inherit from object, that is, object is the base class for all types.
The figure is as follows:
2.2 Custom types
In addition to Python's built-in type, we customize a type MyClass. Similarly, you can get:
2.3 Custom type subclass
Define another type, MySubClass, which is a subclass of MyClass:
2.4 the relationship between type and object
In the above example, we describe the inheritance and type relationships between different objects and types, but the relationship between two special types, type and object, is not pointed out. Let's print it first:
> type (type) > type (object) > type.__base__ > object.__base__ > print (object.__base__) None
You can see that the type of object is also the type of type,type itself and the type of type;, and the parent class of type is also the parent class of all objects-- object, while object itself has no parent class. From this, we can sum up:
Object is the base class of all types (except itself) and is essentially a type, its type is type, and it is also the base class of type.
Type is a type of all types, essentially a type, its type is itself, and it is also the type of object.
Note: object itself cannot have a base class because-for classes that have inheritance relationships, member properties and member method lookups need to backtrack the inheritance chain and keep looking for the base class. Therefore, the inheritance chain must have an end, or there will be an endless cycle.
Finally, we add the relationship between type and object:
3. Mutable objects and immutable objects
The value of a mutable object can be modified after it is created, and its value cannot be modified after an immutable object is created.
Take the integer object in Python as an example: the integer type is immutable and the integer object is immutable. When "modifying an integer object", Python will create a new object with the new value, the variable name will be bound to the new object, and the old object will be released if there are no other references. (the recycling optimization is created through the "small integer pool". Details are described later, which will be supplemented later).
The figure is as follows:
Take the list object in Python as an example: the list type is mutable and the list object is mutable. A dynamic array is maintained inside the list object, and the pointer to the element object is stored. When the list object adds or subtracts the object, the array is modified, while the "header" of the list object (described in more detail later) remains the same:
4 variable length object and fixed length object
Fixed-length object: the memory size of the object is fixed
Side length objects: the same type, different objects will have different sizes
You can see the size of an object through sys.getsizeof ():
> > import sys > a = 1 > b = 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Integer objects are variable-length objects: fixed-digit integers can represent a limited range, so integer objects change their memory size according to their numerical values. In Python, we use the idea of large integer class in C++ to achieve integer object, which supports a larger range of values by concatenating a number of ordinary 32-bit integers (detailed source code later introduction).
Floating-point objects are fixed-length objects: according to the knowledge of the unit, we use 32 bits to represent single-precision floating-point numbers and 64 bits to represent double-precision floating-point numbers. In Python, floating-point numbers are implemented by a double, and even if they represent a large number, the size of the floating-point object remains the same (at the expense of precision). Of course, floating-point numbers also have size limits, so think about this: when we convert a large int to float through float (), will we get an error? Does the bottom layer of Python make a judgment accordingly?
5 supplement
Variable name: when we create an object, we allocate the corresponding memory space for the object, so how do we store the variable when we bind the variable name to the object?
Thank you for your reading, the above is the content of "Python everything object source code analysis". After the study of this article, I believe you have a deeper understanding of the problem of Python everything object source code analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.