

It specifies some operations required for the generation of all classes. In other words, type is the class of all classes, that is, the most basic metaclass. What is type? It is a subclass of object, and object is its instance. One is that the base class of singleton is type, and the other is that there is a metaclass = singleton in the place defined by spam. There are two main differences from general class definitions. Self._instance = super()._call_(*args, **kwargs) The simplest implementation method is this The requirement is that a class can only have one instance. We all know that there are many ways to implement the classic singleton mode. So what about some properties of classes we want to control? Is there such a need? Of course! What if you want to control the properties of the generated instance object? It’s too simple in class definition_ init_ method. But we usually write them in functions, so we divide them like this. However, you can’t be too absolute, because if you instantiate a class in the module scope, the instance object will also be created. The instance object belongs to runtime, and a single import will not create the instance object. After importing a module, they will be created. So we can say that metaclasses and classes belong to importtime.
#Xmind pro 7 vr3.6.1.2015 serial key code#
What about execution? The code in functions and methods is executed. What about class definitions? When a class object is created, the code of the class definition domain is executed, and the code in the class method will not be executed. What about the function definition? A function object is created, but the code in it will not be executed. What happens when a module is imported? Statements (undefined statements) in the global scope are executed. As the name suggests, there are two moments, import time and runtime. Let’s distinguish between two things: importtime and runtime. If we change our perspective, we don’t have to use the same standards as the previous three levels. In short, metaclass is the “class” of “class”, that is, something higher than class. Readers with a little foundation will know the existence of metaclass. In this way, we have instance objects and class objects. Python implemented the type as an object long ago. Usually, we know that an object has its type. Let’s divide the objects into layers first. Let’s talk about something Python programmers can easily get confused. For example, adding attribute methods to objects dynamically is not “metaprogramming” at all in Python, but it requires some skills in some static languages. In addition, python provides many “metaprogramming” mechanisms such as special methods, metaclasses, and so on. 6、 Control the creation of subclasses - methods to replace metaclassesįirst of all, it’s a clich é that everything in Python is an object.
