Python Object-oriented (item series, __enter__ and __exit__,__call__ methods, meta classes)

Source: Internet
Author: User
Tags iterable

Item Series __slots__ method __next__ and __iter__ implement iterator destructor context Management Protocol meta-class
The Item series simulates the object manipulation properties into a dictionary format.
Example: Object name [' key '] = value

Class Foo:    def __init__ (self,name):        self.name = name    def __getitem__ (self, item):        return self.__dict_ _[item]    def __setitem__ (self, Key, value):        Self.__dict__[key] = value    def __delitem__ (self, key):        Self.__dict__.pop (key) F = Foo (' Egon ') f[' age ' "= 18print (f.__dict__) del f[' age ']print (f.__dict__) print (f[' name ']) # Print (f[' sex ')) #执行结果: {' name ': ' Egon ', ' age ': 18}{' name ': ' Egon '}egon

Second, __slots__ method:

What is 1.__slots__: A class variable that can be a list, a tuple, or an iterative object, or a string (meaning that all instances have only one data property)

2. Using a point to access the nature of the attribute is to access the __dict__ property Dictionary of the class or object (the dictionary of the class is shared, and each instance is independent)

3. Why use __slots__: A dictionary consumes a lot of memory, if you have a class with very few attributes, but there are many instances, in order to save memory you can use __slots__ to replace the __dict__ of the instance
When you define __SLOTS__, __slots__ uses a more compact internal representation for the instance. An instance is constructed from a small, fixed-size array, rather than defining a dictionary for each instance, similar to a tuple or list.
The attribute names listed in __slots__ are internally mapped to the specified small label for this array. A bad place to use __slots__ is that we can no longer add new properties to the instance, only those that are defined in __slots__.

4. Note:Many of the features of __slots__ depend on common dictionary-based implementations. In addition, classes that define __SLOTS__ no longer support some common class features, such as multiple inheritance. In most cases, you should only define __slots__ on classes that are often used as data structures, such as millions of instance objects in a program that need to create a class.

5. Benefits: Save Memory: The property is specified within the class, and the object can only establish the corresponding property. There is no more attribute dictionary __dict__, unified __slots__ tube.

A common misconception about __slots__ is that it can be used as an encapsulation tool to prevent users from adding new properties to an instance. Although the use of __slots__ can achieve this goal, but this is not its original intention. More is used as a memory optimizer tool.

class people:    __slots__ = [' x ', ' y ', ' z ']p = People () print (people.__dict__)  #查看类的字典, find a Method! p.x = 1P.Y = 2p.z = 3print (p.x,p.y,p.z) P1 = people () p1.x = 10p1.y = 20p1.z = 30print (p1.x,p1.y,p1.z) print (p1.__dict__) #会报 Wrong! #执行结果: {' __module__ ': ' __main__ ', ' __slots__ ': [' x ', ' y ', ' z '], ' x ': <member ' x ' of ' people ' objects>, ' y ': <membe R ' y ' of ' people ' objects>, ' z ': <member ' z ' of ' People ' objects>, ' __doc__ ': none}1 2 310 30Traceback (most R Ecent call last):  File "method of f:/py_fullstack_s4/day32/__slots__. Py", line +, in <module>    print (p1.__dict __) Attributeerror: ' People ' object has no attribute ' __dict__ '

Three, __next__ and __iter__ implement iterators

From collections import Iterable,iteratorclass Foo:    def __init__ (self,start):        Self.start = start    def __ Iter__ (self): #可迭代方法, turns the object into an iterator, and returns itself to the    __next__:  #对迭代器取值        if Self.start > 10:            raise stopiteration        n = self.start        Self.start + = 1        return NF = Foo (0) print (isinstance (foo,iterable))  #判断是否是可迭代对象print (Isinstance (foo,iterator))  #判断是否是迭代器print (Isinstance (f,iterable))    # Determine if the object is an iterative print (Isinstance (f,iterator))    #判断是否是迭代器for i in F:    print (i) # Execution Result: FalseFalseTrueTrue012345678910

Define the class and implement the range () Method!

Class Range:    def __init__ (self,start,end):        self.start = start        Self.end = end    def __iter__ (self):        Return self    def __next__ (self):        if Self.start = = self.end:            raise stopiteration        n = Self.start        Self.start + = 1        return nfor i in Range (0,3):    print (i) #执行结果: 012

Callable () determines whether a class can be called

__doc__ description information, this property cannot be inherited, is the description of this function or class! Did not write print none

__class__ and __module__
__MODULE__ indicates which module the object of the current operation is in
__CLASS__ represents the class of the object that is currently being manipulated

Class Foo:    passclass A (foo):    Passa = A () print (a.__class__) print (a.__module__) #执行结果: <class ' __main__. A ' >__main__

Five, __del__ destructor

When the object is deleted or the object is reclaimed, the reference base is 0, and the execution is automatically triggered to release the object in memory.
Note: This method is generally not defined because Python is a high-level language, and programmers do not need to be concerned with allocating and releasing memory because this work is done by the Python interpreter, so the destructor calls are automatically triggered by the interpreter when it is garbage collected.

Defining __del__ in a class is typically a write-down, or a shutdown of a database. As long as the function is not fully operational, the memory will not be reclaimed, and the person who recycles it is done.

Import Timeclass Open:    def __init__ (self,filepath,mode = ' r ', encode = ' utf-8 '):        self.f = Open (filepath,mode= Mode,encoding=encode)    def write:        pass    def __getattr__ (self, item):        return GetAttr (SELF.F, Item)    def __del__ (self):        print (' =---->del ')        self.f.close () F = Open (' a.txt ', ' r+ ') #没有删除命令, also automatically executed! When there is no code to execute, the reference base is 0, which is triggered. # f1 = f  #定义其他的引用 # del f #先执行删除命令, then print the contents of print (' =-====> ') #执行方式: # =-====># =---->del# Execute the Delete command first, then print the contents below del F #先执行删除命令, the print (' =-====> ') #没有删除命令 on the bottom is also automatically executed! When there is no code to execute, the reference base is 0, which is triggered. #执行方式: # =---->del# =-====> #引用基数不为0的情况f1 = f  #定义其他的引用del f #先执行删除命令, then print the contents below (' =-====> ') #执行方式: # =-== ==># =---->del

Vi. Context Management protocol (with method)
__enter__ and __exit__
Use or Benefits:

1. The purpose of using the WITH statement is to put the code block in with, and with the end, the cleanup work is done automatically without manual intervention

2. In a programming environment where you need to manage resources such as files, network connections, and locks, you can customize the mechanism for automatically releasing resources in __exit__, which is useful if you don't have to relate to the problem.

# __enter__ and __exit__class Foo:    def __enter__ (self):  #with get an object that triggers the Enter method        print (' Enter ')        return 11111    def __exit__ (self, exc_type, Exc_val, EXC_TB): #文件打开执行完毕后, execute Exit method        print (' exit ')        print (' Exc_type ') , Exc_type)  #异常的类型        print (' Exc_val ', exc_val)    #异常的值        print (' Exc_tb ', EXC_TB)      #异常的内存地址        Return True #清空所有异常, after throwing an exception, the content after with is executed normally. With Foo () as obj:  #执行Foo (). The __enter__ method gets a return value and assigns the value to obj.    #出现with语句, the __enter__ of the object is triggered, and the return value is assigned to the variable    print as declared (' Child code block with Foo ', obj) #执行with模块字代码    raise Nameerror (' The name is not defined! ')  #只要是报异常, without processing, means that the word code block with execution runs out. Triggers the exit method, after which the code is no longer executed.    print (' ############## ') print (' ************** ') #执行结果: Enterwith Foo's child code block 11111exitexc_type <class ' Nameerror ' >exc_val name is undefined! Exc_tb <traceback Object at 0x00000000028ee948>**************

Example: Implementing the context of the Management Protocol: (with method Open File execution operation!) )

Class Open:    def __init__ (self,filepath,mode,encode= ' utf-8 '):        Self.f=open (filepath,mode=mode,encoding= Encode)        Self.filepath=filepath        self.mode=mode        self.encoding=encode    def write (self,line):        Print (' write ')        Self.f.write (line)    def __getattr__ (self, item):        return GetAttr (self.f,item)    def __ Enter__ (self): #with object will trigger the method under the object        return self  #将对象返回 write_file=open (' Aaaaa.txt ', ' W ')        #return Self.f #这就是返回真实的open方法, the word code block method can be used, but it is no longer the use of class open.    def __exit__ (self, exc_type, Exc_val, EXC_TB): #文件结束清理        self.f.close ()        return truewith Open (' Aaaaa.txt ', ' W ') as Write_file: #变相的实例化 write_file=open (' Aaaaa.txt ', ' W ') get the resulting file handle    write_file.write (' 123123123123123\n ')    write_file.write (' 123123123123123\n ')    write_file.write (' 123123123123123\n ')

Vii. Methods of __call__

The object is appended with parentheses to trigger execution.

Note: The execution of the construction method is triggered by the creation object, that is: Object = class name (), and the execution of the __call__ method is triggered by parentheses after the object, i.e.: Object () or Class () ()

Class Foo:    def __init__ (self,name):        self.name = name    def __call__ (self, *args, **kwargs):        print (' __ call__ ') obj = Foo (' Egon ')  # executes __init__#obj ()  # executes __call__ object pair to find out if there is no __call__ binding method, plus () will be able to run! #查看是否为可调用对象 (Callable object: The name is added () to run) print (callable (Foo)) print (callable (obj)) #执行结果: truetrue# If the __call__ function is hidden, view the result class Foo:    def __init__ (self,name):        self.name = name    # def __call__ (self, *args, **kwargs):    #     print (' __call__ ') obj = Foo (' Egon ')  # executes __init__#obj ()  # executes __call__ object pair to find out if there is no __call__ binding method, plus () will be able to run! #查看是否为可调用对象 (Callable object: The name is added () to run) print (callable (Foo)) print (callable (obj)) #执行结果: TrueFalse

Eight, meta-class

1 intro

1 class Foo:2     PASS3 4 f1=foo () #f1是通过Foo类实例化的对象

  everything in Python is an object, and the class itself is an object, and when you use the keyword class, the Python interpreter creates an object when it loads the class. (The object here refers to an instance of a class rather than a Class)

The above example shows that F1 is an object produced by the class Foo, and that Foo itself is an object, and which class is it produced by?

1 #type函数可以查看类型 can also be used to view the object's class, both of which are the same 2 print (type (f1)) # output: <class ' __main__. Foo ' >     indicates that the Obj object was created by the Foo Class 3 print (Type (Foo)) # Output: <type ' type ' >  

2 What is a meta-class?

The Meta class is the class class and is the template for the class

a meta-class is used to control how a class is created, just as a class is a template that creates objects

An instance of a meta class is a class, as an instance of a class is an object (The F1 object is an instance of the Foo class , and theFoo class is an instance of the type class )

Type is an inner Jianyuan class of python that is used to directly control the generated class, and any class defined in Python is actually an object instantiated by the type class.

3 Two ways to create a class

Method One:

Class Foo:    x =1    def run (self):        passprint (foo) print (Type (foo))

Method Two:

#type称为元类, is the class of all classes that control the class. The process of creating classes using the Type simulation class keyword. def run:    print ('%s is running '%self.name) class_name = ' Bar '  #类名bases = (object,)     #继承方式class_dic = {' X ': 1, ' Run ': Run}  #名称空间 attribute method # syntax: variable name = Type (class name, inheritance relationship (tuple), attribute Method (dictionary)) class keyword Creates a class that is essentially the type () encapsulated Method! Bar = Type (class_name,bases,class_dic)  #自定义生成一个类print (bar)  #查看类型print (Type (bar)) #查看元类

4 A class does not declare its own meta class, default his meta class is type, in addition to using the meta-class type, the user can also inherit the type to customize the meta-class (by the way we can also look at the meta-class how to control the creation of classes, the workflow is what)

(1) Add some conditional judgment method to class by meta-class (view workflow)

Class Mymeta (Type): #定义元类继承type def __init__ (self,class_name,class_bases,class_dic): #实例化就会调用执行默认的方法, and use the type to create a direct Build a class consistent.          (class name, inheritance relationship, method) print (self) #对比打印验证 print (class_name) #对比打印验证 print (class_bases) #对比打印验证 Print (class_dic) #对比打印验证 #对创建类, the person who does not write __doc__ method prompts for key in Class_dic: Judgment of #可在元类初始化这里加上对下边 subclass  Validation method if not callable (Class_dic[key]): Continue #判断类方法中有没有可调用方法, no continuation if not class_dic[key].__doc__: #判断方法类中__doc__对应的值是否为None Raise TypeError (' Boy, you didn't write the notes, go and write ') # type.__init__ (self,class_name,class_bases,class _dic) #执行的上述方法实际上就是在调用type () method class Foo (Metaclass=mymeta): #将类foo看成元类的对象, plus () running is instantiating X=1 def run (self): ' R Un function ' print (' running ') # Foo=mymeta (' Foo ', (object), {' X ': 1, ' Run ': Run}) #上述利用class方法定义Foo类与用type () definition is consistent. Print (foo.__dict__) #执行结果: <class ' __main__. Foo ' >foo () {' __module__ ': ' __main__ ', ' __qualname__ ': ' foo ', ' x ': 1, ' Run ': <function Foo.run at0x00000000022bc950>}{' __module__ ': ' __main__ ', ' x ': 1, ' Run ': <function foo.run at 0x00000000022bc950> ' __ dict__ ': <attribute ' __dict__ ' of ' foo ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' foo ' objects>, ' __doc__ ': None}

(2) A meta-class is used to define the complete process of producing a class:

Class Mymeta (type): #定义元类     def __init__ (self,class_name,class_bases,class_dic): #始终明确 Self is foo            pass # Do not define the internal method of walking type ()     def __call__ (self, *args, **kwargs): #类实例化就能调用执行其实质上是执行了__call__方法        # print (self)        obj= Self.__new__ (self)  #类Foo实例化f, Foo executes the __new__ method, first producing an empty object        self.__init__ (Obj,*args,**kwargs) #obj. Name= ' Egon '            #上一行代码是在 to the resulting empty object, call the __init__ method under Foo, that is, the __init__ method in the defined class Foo returns   to obj #空对象拿到值之后, producing a new object, obj, Then get this value and return this value. Class Foo (Metaclass=mymeta):    x=1    def __init__ (self,name):  #类内给实例化对象定义的初始属性        Self.name=name # Obj.name= ' Egon '    def Run (self):        ' run function '        print (' running ') # print (foo.__dict__) f=foo (' Egon ')  #类实例化print (f) Print (F.name) #执行结果: <__main__. Foo Object at 0x0000000002280128>egon

Note the point:

#元类总结class Mymeta (type): Def __init__ (self,name,bases,dic): Print (' ===>mymeta.__init__ ') def __new__ (CLS, *args, **kwargs): Print (' ===>mymeta.__new__ ') return type.__new__ (Cls,*args,**kwargs) def __call__ (sel F, *args, **kwargs): Print (' AAA ') obj=self.__new__ (self) self.__init__ (Self,*args,**kwargs) re Turn objclass Foo (Object,metaclass=mymeta): def __init__ (self,name): Self.name=name def __new__ (CLS, *args, * *kwargs): Return object.__new__ (CLS) "needs to be remembered: the nature of the parentheses in the name (i.e., any form of name) is to find the father of name first and then execute: Dad. __call__ and dad. __call__ Generally do two things: 1. Call the Name.__new__ method and return an object 2. Then call the Name.__init__ method to initialize the name of the son "" "class to define Foo, and to specify the meta class as Mymeta, This is equivalent to using Mymeta to create a new object Foo, which is equivalent to executing foo=mymeta (' foo ', (...), {...}) So we can see that just defining class will have the following effect ===>mymeta.__new__===>mymeta.__init__ actually class Foo (Metaclass=mymeta) is triggered foo= Mymeta (' Foo ', (...), {...}) Operation, encountered the name of parentheses in the form of Mymeta (...), so go to find Mymeta's father type, and then execute type.__call__ (...) The method then triggers the mymeta.__new__ method to get a specific object and then triggers the MymeThe Ta.__init__ method initializes the object with the principle "'" ' Obj=foo (' Egon ') to summarize: the difficulty of the meta-class is that the execution order is very round, in fact, we just need to remember two points on the 1. Who's behind the brackets, looking for __call__ from whose father? Method performs a type->mymeta->foo->objmymeta () trigger Type.__call__foo () trigger mymeta.__call__obj () trigger foo.__call__2.__call__ The __new__ and __init__ methods of the son are called in order of succession.

Python Object-oriented (item series, __enter__ and __exit__,__call__ methods, meta classes)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.