Objects, classes, metaclasses in Python

Uncle Turtle invented Python, and then integrated a bunch of concepts into the language, such as iterators, decorators, functions, generators, classes, objects, coroutines, and more.
None of these concepts seems to be easy for beginners to understand, but there are more difficult concepts. It is the creator of the Python world. Although we rarely use it directly, we use it every day. It is the protagonist of today. -- Metaclasses.
To understand metaclasses, let's start with objects.

Object

Everything in Python is an object. You must have heard this sentence (you have heard it now), a number is an object, a string is an object, a list is an object, and a dictionary is an object, for example:
i=10
s='abc'
nums=[1,2,3]
dicts={"name":"zeng"}
The right side of the equal sign is the object, and the left side is the name given to these objects. Any object has three key properties: identity, value, and type.
The ID
is the same as the ID of a person's ID card. Each object has a unique ID, which will not change during the entire life cycle. You can think that the ID is the address of the object in the computer memory. The ID of the object can be viewed through the function id().
print ( " The unique identifier of the object is: " + str ( id (i)))
The unique ID of the object is: 1704834400
print ( " The unique identifier of the object is: " + str ( id (dicts)))
The unique ID of the object is: 1918903679592

The second attribute of the object value object is the value, and the value is well understood. For example, the value of i is 10, the value of s is abc, and the value of nums is 1,2,3. Another important attribute of type objects is the type. Any object has its own type, and the object is constructed from its type
. For example, the type of i above is an int type, the type of s is a string type, the type of nums is a list type, and the type of dicts is a dictionary type, all of which are constructed from the corresponding types. Use type() to check the type of an object.



print ( " The type of the object is: " + str ( type (i)))
print ( " The type of the object is: " + str ( type (dicts)))
The type of the object is: <class 'int'>
The type of the object is: <class 'dict'>

The type of the object does not change like the ID tag. The only thing that can change is the value.

kind

In addition to the integer types, string types, lists and other types already defined by the system, we can also create our own types and define them with the keyword class. E.g:

class Person ( object ) :
 #__init__() is kind of like a constructor, calling
 def __init __ ( self , name , gender ) when instantiating the class :
 #name is the instance's property
 self .name = name
 self .gender = gender
 #live is the class attribute
 live = True                                    

Person here is a custom class, a class is an abstract template, neither refers to Zhang San nor Li Si and other specific people, now we can call this class to construct (instantiate) a concrete, real, An object with a name comes out, and this object is called an instance object (Instance).

Classes and (Instance) Objects

 
 
p1 = Person ("zhangan","")
print(p1.name)
p2 = Person ("lisi","")
print(p2.name)
zhangan
lysis

Here p1 and p2 are the instantiated (instance) objects. The types of these two objects are both of the Person class. The relationship between the class and the (instance) object is like the relationship between a vehicle mold and a built real car. Same relationship. As follows:

print(p1)
print(id(p1))
print(type(p1))
<__main__.Person object at 0x000002031271BBE0>
2212217600992
<class '__main__.Person'>

Classes are Objects (aka Class Objects)

Just now we said that everything is an object, an instance (real car) is an object, and a class (a mold car) is of course an object, because it is also a real thing.

When the Python interpreter executes the keyword class instruction, it will create a class named "Person" internally. This class is also an object, which we call a class object (note the difference between instance objects), which also has ID identification, type, and value. E.g:

print(Person)
print(id(Person))
print(type(Person))
<class '__main__.Person'>
2446546123192
<class 'type'>
We noticed that the type of the Person class object is called "type", which means that the Person class is created by type. Now you have to remember that p1 and p2 are instance objects, while Person is a class object, and the type of the instance object p1 is the class object Person, and the type of Person is type. Also, what the hell is this type?

Let's review:

i = 10
 print ( " The type of the object is: " + str ( type (i)))
 print ( " The type of the object is: " + str ( type ( int )))
The type of the object is: <class 'int'>
The type of the object is: <class 'type'>
The type of i is int, the type of int is also type, and the type of all classes is type, that is to say, all classes are created by type. This type is a metaclass. A metaclass is a class used to create a class. Dao produces one, one life two, and three produces all things. Metaclass is the creator in Python. (The metaclass is itself an object)

Now we all know that a class (object) can be created using the class keyword, and we also know that the type of a class (object) is type. Now that we know that its type is type, we can definitely create it through type (metaclass).

Create classes with metaclasses

As mentioned earlier, type has one function to check the type of an object. In fact, it has another function to dynamically create a class (object) as a metaclass.

Person1 = type("Person1", (), {"live":True})
print(Person1)

Person is a class that is equivalent to:

class Person1:
    live = True
print(Person1)
<class '__main__.Person1'>
The syntax for creating a class with a metaclass type is:

type (class name, parent class tuple (can be empty), attribute dictionary)

Summary
Everything in Python is an object, a class is an object, a metaclass is an object, and a metaclass is a class used to create a class.


This article refers to the public account [python Zen], thank you very much!
Link: https://mp.weixin.qq.com/s/UTFQgu4q5pHFyQZBWA9qpA

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324573318&siteId=291194637