[New-style class "new class (New STYLE CLASS)

zhaozj2021-02-16  107

Class (Class) is also an object in Python, all objects. Class is also an object. "Class class" is called a category (ie, an example of a class class). As the behavior of the examples, the behavior of instances (classes) of the class and the class can also depend on the class.

The origin of New-Style Classes is an important feature that Python is joined in version 2.2. All built-in types are New-Style Classes. Introducing new-style classes, is to gradually bridge the gap between the build types and Classic Classes in Python. At this point, the built-in type can finally be inherited as the base class as the class class as the classic class. But before it is completely unified, there is still a huge difference. Its root cause is different of their respective metacles: Classic Classes's mid-class is types.classtype, and New-style classes is Type. In other words, we can make such definitions: class class class is the class of classic classes; class class is Type class is New-style classes. In addition, according to the following conclusions, we can also give another definition of New-style classes: Object and all classes that directly or indirectly use it are new-style classes. The Object here is a New-style class built-in type, which is a public base class for all new-style classes (see below).

New-style classes creates a class object of Python directly by the Class statement. So how is the class statement to determine the category of the class object it wants to generate? The answer to this question, can be drawn from the working principle of the class statement: the interpreter first obtains the information of three aspects: class name, class base class, class's properties and methods. The base class of the class is placed in a tuple, and the properties and methods of the class are placed in a DICT. Next, the interpreter can be determined by this information: "__metaclass__" in the class property is "__metaclass__", then the object it binds is a category. Otherwise, if the class defines the base class, then use the base class Metacity (as long as there is new-style classes in the base class, the class class is TYPE. Otherwise, if there is a name "__metaclass__" in the module in the class, the object it binds is a category. Otherwise, The class class of the class is determined by default to Types.ClasStype, the interpreter is passed to the class name, class, class method, and attribute (DICT) to the yuan to generate it One example of the class class (ie, class). Finally, the interpreter is bound to the class name with the generated class object. All other built-in types (ie, non-Object), becomes new-style classes by inheriting a common new-style base class Object. From the above description, we can get a way to create a new-style class, a total of three: 1. Inherit an built-in type >>> Class MyDict (dict): pass >>> type (mydict) 2. Setting the __metaclass__ attribute >>> class mynewstyle1: __metaclass__ = type >>> type (mynewstyle) 3. Set a module level __metaclass __ >>> __metaclass__ = type >>> Class MyneWStyle2: Pass >>> Type (MyNewStyle2) There is also a point worth noting that NEW-style classes created in the above way will eventually or indirectly use Object as base Class (here reflecting the control of classifications). That is, even if the inherit syntax is not directly written like the method 2, 3, Object will become a base class: >>> mynewstyle1 .__ bases __ (,) >>> mynewstyle2 .__ Bases __ (,) Of course, if the conditions of the creation new-style classes present are not satisfied, you will create a original Classic Class >>> DEL __METACLASS__ # Eliminate the previously introduced module level influence >> Class MyClassic: Pass >>> Type (MyClassic) >>> myclassic .__ Bases__ ()

New-style classes uses new-style classes to provide some new special methods to better control the behavior of classes. Below, we speak several common: 1.________ to control the initialization of the instance of the class, and __New__ control before initialization --- is the generation of the instance. Therefore, the first parameter of __new__ is class (CLS), not Self. __New__ task is to return an instance of the incoming parameter class (CLS). However, it is really possible to generate an instance of new-style class, only object .__ new__. So we define __new__, you should call it when you need to generate an instance. With the characteristics of __new__, we can easily implement Singleton Pattern, let our classes only produce a unique object >>> Class Singleton (Object): __instance = none def __new __ (CLS, * args, ** kWd : If Singleton .__ instance is none: singleton .__ instance = Object .__ new __ (cls, * args, ** kwd) Return Singleton .__ instance >>> class myclass (singleton): pass >>> a = myclass () >> > b = myclass () >>> a is btrue >>> A is b is b is b is b is b is b is b. By inheritance, you can make the behavior of the class in accordance with the behavior of the class of Singleton Pattern 2.__ getattribute__: __getattribute__ ratio __getattr__ A more powerful control is provided for the read operation of the properties and methods of the object using ". Syntax" (such as obj.a). Any obj.a will be directly converted to Obj .__ getattribute __ (a), including __dict__. Therefore, in the implementation of __getattribute__, we can't use it directly to use __dict __ (which will cause loop calls), and the true read operation should be handed over to Object .__ GetAttribute__. >>> Class MyClass (Object): Def __getattribute __ (self): print "accessing", name return object .__ getattribute __ (self, name)

>>> a = myclass () >>> a .__ DICT__Accessing __dict __ {}

3 .__ get __, __ set __, __ delete__: Overloaded these three methods, you can implement read and write control for class properties Class Revealaccess (Object): "" A Data Descriptor That Sets and Returns Values ​​Normally and Prints a Message Logging Their Access. "" "

Def __init __ (self, initval = none, name = 'var'): self.val = initval self.name = namedef __get __ (self, obj, objtype): print 'retrieving', self.name return self.val

DEF __SET __ (Self, Obj, Val): Print 'Updating', Self.name Self.val = VAL

>>> Class MyClass (Object): x = Revealaccess (10, 'var "x"') y = 5

>>> m = myclass () >>> M.XRETRIEVING VAR "X" 10 >>> MX = 20Updating var "x" >>> M.XRETRIEVING VAR "X" 20 >>> M.Y5 can be built Function Property, simplifying the above steps >>> Class MyClass (Object): Def __init __ (self): self.__ x = 1 Def getx (self): Print "Retrieving X" Return Self .__ x DEF SetX (Self, Value): Print "Updating x" self.__ x = value def DELX (Self): Print "DELETING X" DEL Self .__ x = Property (Getx, Setx, Delx, "I'm THE x Property" >>> m = myclass >>> M.XRETRIEVING X1 >>> MX = 2UPDATION X >>> DEL M.XDELETING X

For more features and methods of use of new classes, please refer to: Unifying type and classes in Python 2.2how-to Guide for Descriptors

After the report, I am also a Python beginner, and the confusion and harvest records in the study are exchanged, and I will communicate with you, I hope to get everyone's opinion.

转载请注明原文地址:https://www.9cbs.com/read-10200.html

New Post(0)