In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what is the use of type and object in python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Before we begin
The most important thing is to understand the difference and relationship between type and object. We usually use Object most often. For example, when you define a class, you will inherit object:
Here you define a custom class Test, it is not difficult to see that Test inherits object, that is, object is a superclass (or base class) of Test.
Next, you can define another class:
SubTest inherits Test, and because Test inherits object, subTest inherits object. An important knowledge point is involved here, that is, inheritance is transitive. If you look closely, you will find another knowledge point: object is the superclass of all classes (this sentence is very important). So what is type? It is the type of object (that is, object is an instance of type), and object is a superclass of type.
The phrase "type is the type of object, and object is the superclass of type" seems to be full of doubts: is there object first or type first? In fact, "object and or type problem" is like "chicken or egg problem first". Who was it first? There is no hurry, please continue to read:
To understand this, you need to know that python is an object-oriented language. In python, everything is an object.
Everything is an object? It may not be easy for some people to understand. Let's put it this way, in python, int × × is an object, the integer 2 is also an object, the functions and classes you define are all objects, and the variables you define are also objects. In short, everything you can use in python can be called an object.
Well, after figuring out that everything in python is an object, you need to understand that in an object-oriented system, there are two relationships:
Parent-child relationship (described by solid lines in the figure): this relationship exists when one class (subclass) is a special version of another class (superclass). It is usually described as "a subclass is a parent class". For example, a snake is a reptile (Snake is a kind of reptile). Among them, snakes (snake) are subclasses and reptiles (reptile) are parents. A snake has the characteristics of a reptile, but at the same time, it also has the characteristics that it is a snake.
Type instance relationship (described by dotted lines in the figure): this relationship exists in two objects, one of which (instance) is the concrete implementation of the other object (type). I have a pet snake called Squasher, so Squasher is an example of a snake. The English description is "Squasher is an instance of snake".
The relationship between father and son is represented by solid lines because the relationship between father and son is more "solid". For example, when someone asks you to list words about snakes, you may say that snakes are reptiles, but you won't say that snakes are Squasher.....
I think it would be more intuitive if you expressed the above two relationships in code:
Class reptile (object) and class snake (reptile) represent the father-son relationship. Object is the base class of reptile, and reptile is the superclass (base class) of snake. Do you remember that object is the superclass of all classes?
Squasher = snake () is the type instance relationship. Instantiating the class snake results in Squasher.
At this point, there are two very useful rules:
Dashed Arrow Up Rule:If X is an instance of A, and An is a subclass of B, then X is an instance of B as well. The translation should be "dotted line upward rule": if X is an instance of An and An is a subclass of B, then X is also an instance of B.
Dashed Arrow Down Rule:If B is an instance of M, and An is a subclass of B, then An is an instance of M as well. The translation should be "dotted line downward rule": if B is an instance of M and An is a subclass of B, then An is also an instance of M. In fact, this rule is rarely used, but it is closely related to the content of this blog. Let me make a brief analysis. From the sentence "if B is an instance of M", B is an instance, and "An is a subclass of B"-> B is a (parent) class. B is an instance and a class at the same time? What's going on? After reading this blog, you will know the answer.
Here, let me explain why it is called "dashed line upward rule". By looking at the right side of the image above, we can clearly see a dashed line with an arrow, starting from the X end and shooting toward the An end. At this time, the An end is the arrow end, and the dotted line represents the type instance relationship of the type, so the A side is the type, that is, X is the instance of A (in other words, An is the type of X). We can check the type of X by using the command X.arrow classrooms. Again, a solid line with an arrow shoots from the An end to the B end, the B end is the arrow end, and the solid line represents the parent-child relationship, so the B end is the parent class, that is, An is a subclass of B. At this point, we achieve the purpose of expressing that X is also an example of B by shooting the X end to the dotted line at the An end, up to the B end (you should see that there is an upward dashed line marked implied at the top right of the image above). It is also worthy of the name, the dotted line goes up. The dotted line downward rule can also be pushed out in this way, so I won't demonstrate it.
Generally speaking, in the object-oriented system, there are two kinds of relations, one is parent-child relationship, which is described by parent class and subclass, and the other is type instance relationship, which is described by class and instance. The two rules, however, relate the relationships between classes and between classes and instances.
At this point, we can get to the topic.
Basic concept
Object interior: The Object Within
We talked about object-oriented above, so what is an object? Object is one of the important cores of python: it is an abstract description of an entity. The object has the following characteristics:
Identity: given two names, I can say for sure that either they point to the same object or they are not
Value (A value): this means that the object contains a bunch of properties. We can manipulate properties through objectname.attributename.
Type (A type): each object has an exact type. For example, the type of object "2" is int
One or more "Bases" (One or more bases): not all objects have Bases, but some special objects do, such as classes. Bases is similar to the "base class" and "superclass" in object-oriented languages.
If you want to know the location of an object in memory, you can call id (object) to see. Here, I emphasize once again that in python, everything has the concept of object. The number 2 is an object, and the type int is also an object.
Type and Bases (if they exist) are important because they define the relationship between one object and another. Keep in mind that type and Bases are objects themselves, which will be mentioned later.
You might think that the object has a name, but the name is not part of the object. The name of the object exists outside the object's namespace (namespace) or is a property of another object. That is to say, the name and the object are not stored in the same "place".
Example: testing an integer object
(1): we assign the number 2 a name in the current namespace. And bind 2 to "two".
(2): the type of this object is. You'll see output like int in other places, but they all mean the same thing.
(3): well, the type is.
(4): output the base class of "class int".
(5): list all the properties of the integer object.
It may feel a little messy, but let me sum it up a little bit: the number 2 is an instance of type int (generally speaking, "class" and "type" are the same thing in python). On the other hand, int is the type of number 2. The tuple (,) is the superclass (or parent class) of type int. There may be more than one superclass of a type, so it is represented as a tuple.
Now, let's introduce the first rule:
Everything is an object.
The number 2 mentioned above and the superclass of type int,int are all objects. In addition, the functions and methods you define. They're all dates.
A clean drawing board
Now let's build the object system of python. Starting with a clean artboard, the artboard is divided into three parts, from left to right, representing the class, the class, and the instance of the class.
We will start our learning journey in this drawing board.
Relationship (Relationships)
When we talk about objects, we use two relationships to connect various objects so that there is a connection between them:
Father-son relationship (the subclass-superclass relationship)
Type instance relationship (the type-instance relationship).
These two relationships have been discussed in detail at the beginning of the article.
Enter object (Bring In The Objects)
The first object
Let's test two objects: object and type:
Example 1:
(1), (2): the names of the two source objects in python. We said earlier that type () is used to get the type of object. In fact, it is both an object and a method to get the types of other objects.
(3), (4): view the type of object. Seeing that object is an instance of type, we also use. _ class__ to verify that it is the same as the output of type ().
(5): object does not have a superclass because it itself is a superclass for all objects.
(6), (7): output the type and superclass of type, respectively. That is, object is a superclass of type. The type of type is itself.
We describe the information obtained in example 1 on the artboard:
Object and type are two source objects in python. When we try to introduce them, we will get caught up in the "chicken or egg" problem. Who will introduce them first? In fact, they depend on each other for definition, so they cannot be separated.
Continue our python experiment:
(1): what happened? In fact, the dashed line upward rule is used here. Type is a subclass of object, and the instance of type is naturally also an instance of object. Object is an example of type.
(2): the English document I refer to here explains that the rules of dashed line up and dashed line down are applied at the same time. But I look so confused. Because I think it's the same here as (1): type is a subclass of object, and an instance of type is naturally an instance of object. Type is also an example of type.
If you think the above explanation is confusing, ignore it. It does not affect your understanding of the main purpose of this article.
New concept: type objects
Both type and object belong to type objects. Type objects translates to be a type object. Characteristics of type objects:
They are used to represent abstract data types in a program. For example, we define a class called User that represents all users in the system. Int represents all the × × numbers in the system.
They can be inherited. This means that you can use existing type objects to create new type objects. The existing type object is the superclass of the new type object.
They can be instantiated. This means that you can create new instance objects using existing type objects. The former is the type of the latter.
The type of type object is type
They are sometimes called types and sometimes called classes.
You read it right. In the new version of python, classes and types are already the same thing. It can be seen in a very obvious place. The output of _ _ class__ and type () is the same.
In the old version of python, a class refers specifically to something created with class statements. A built-in type such as int is generally not considered a class, but a type. But in the new version, they are the same thing. I think it is necessary to define a rule for this change:
A class is a type, and a type is also a class (Class is Type is Class)
In the > = 2.3 version of python, classes and types are the same thing.
The term type is equivalent to the term class in all version of Python > = 2. 3.
Both types and non-types (or classes and non-classes) are objects, but only types can be inherited. A non-type has a specific value, so it makes no sense to be inherited, and it cannot be inherited. A simple example is the type int and its instance 2. Int is a type and 2 is a non-type. Tell me, what's the point of inheriting 2?
Do you still wonder whether the society is the type? What is non-type?
Here is a rule of judgment for you:
If an object whose type is "", then it is a type, otherwise it is not.
Remember how to judge the type of an object? That's right, _ _ class__ and type () you can use whatever you want.
Small summary:
The type of is
The parent class of is empty
The type of is itself.
Yes subclass
In python, there are only two kinds of objects: type and non-type. A non-type is also called an instance. Here is the original sentence in English. I don't know how to translate it. It's easy to understand, but I don't know how to say: There are only two kinds of objects in Python: to be unambiguous let's call these types and non-types. Non-types could be called instances, but that term could also refer to a type, since a type is always an instance of another type. Types could also be called classes, and I do call them classes from time to time.
Note that we only draw the direct relationship between the two objects in the artboard, but not the hidden relationship, saving our energy and artboard size.
More built-in objects
Python there are more than two source objects on board. A bunch of objects can be bred from these two source objects:
Figure 2.2. Some built-in objects
Some of the built-in types in the figure above are tested with examples:
(1): built-in object list
(2): the type of list is
(3): the superclass of list is (,)
(4): the type and superclass of built-in object tuple.tuple are:, (,)
(5) mylist, an instance of list
(6) the type of instance mylist is
(7) there is no superclass for the instance.
We can create an instance of tuple or dict, but we cannot create an instance of mylist. Because mylist is not a type, it is just an instance.
Generate new objects through inheritance
The built-in type is inherent in python. So how do we create a new type?
A new type cannot be created out of thin air, it must rely on an existing type, so inheritance is about to emerge.
Example: generating new objects through inheritance
(1): the class statement tells the python interpreter to create a new type from an existing type
(2): in python3.x, you can omit (object).
(3): multiple inheritance
(4): most built-in types can be inherited, but not all of them.
Generate new objects by instantiation
Example 2.5.
(1), (2): create an instance of a type by using the type name (). There may be parameters in ()
(3): this is the syntax for python to create instances using built-in types. There's nothing to say.
Note: type C automatically becomes an instance of simply by subclassing. The reason is in the second frequently asked question.
After the above operation, the original blank drawing board can be full:
Frequently asked questions
There may be a lot of doubts in your mind at this point. Here are some of the questions and answers. Please enjoy them as appropriate. If you have anything mentioned, please leave a message, I will try my best to search for the answer:
Q: how does Python actually create a new object?
A: in python, there are two kinds of new objects created: typed and untyped. Types can be inherited and instantiated. Untyped skills are an example. When python creates a new object, it uses its own type as the type of the new object. Two methods, _ _ new__ () and _ _ init__ (), are generally used. So. Every object has a type.
Q: specify a type when instantiating, but how does python know which type to use when inheriting?
It looks at the superclass you inherited and uses the type of the superclass as the type of the new object.
In most cases, the type of the subclass (subclass of and so on) is
Q: can I create a new type?
A: yes, so the metaclass comes out, and you can recreate a type through the attribute _ _ metaclass__. Here I would like to give a brief example. The metaclass is briefly introduced below.
By inheriting type, we create new types.
Q:wow! Can I use any type as a parameter to metaclass?
A: no. Only classes that inherit type can be used as parameters to metaclass.
Q: should I use metaclass?
Use is not recommended. Except for the master.
Ready to end.
A diagram depicting python objects
We ended up with a map of different objects:
In most cases, we learn the contents of the second and third columns. As for the first column, that's the metaclass domain. Not everyone has to study deeply.
To explain what's in the picture above:
A dashed line can pass from one column to another, for example, from the column where the instance is located to the column where the class is located. (exception)
Solid lines cannot pass through other columns. Once again,-> is the exception.
Solid lines are not allowed in the third column. Because solid lines represent inheritance. The examples of the third column cannot be subclassed.
Objects in the third column are not allowed to be instantiated.
The first, two columns contain types, and the third column contains non-types.
If you create an inherited object, it will be placed in the first column, the metaclass. It continues to be emphasized that classes and types are the same. It's the same with Riley.
Note: all types are types. It is also a superclass for all objects (except itself).
Summary
These contents are a summary of the previous ones:
There are two kinds of objects in python:
Type objects: can be instantiated and inherited
Untyped objects: cannot be instantiated and inherited.
And are two source objects in python.
Every object has a type. Use objectname.__class__ to view.
Every type of object has a superclass (except object), which can be viewed with objectname.__bases__.
New objects generated by inheritance are all type objects. Inheritance is implemented with class statements.
The new object produced by instantiation may be either typed or untyped. If you look at the figure below, the dotted line represents instantiation, and the new objects instantiated in the first and second columns are type objects. The new object produced by the instantiation of the third column is an untyped object. Instantiation is achieved by calling the operator (). For example, if you customize a class myclass, instantiation is accomplished by adding the () operator after myclass. That is, instance_of_myclass=myclass ().
Some untyped objects of python can be created with special syntax. For example, [1,2,3] is an instance of list.
Internally, python always uses type objects to create new objects. The newly created object is an instance of that type of object. Here, an instance has two meanings: a subclass generated by inheritance, and a concrete instance generated by instantiation. But usually the example we are talking about is only the second). Python determines the type of the new object by the type of the superclass specified in the class statement.
Issubclass (A _ maeb) returns true if and only if:
B is in the tuple of the output of A.
If An is in the tuple of the output of Z. benchmark basesprints _, issubclass (ZMagne B) returns true.
Isinstance (A _ maeb) returns true if and only if:
A.classic classrooms _ is B, or
Issubclass returns true.
This is the end of the article on "what is the use of type and object in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.