1, def __add__ (self,other): C1+C2 Two instances of the addition operation is the execution of __add__ () Method 2, __str__ (self):p rint An instance, the implementation of __STR__ () this built-in method Eg:class Vector (object): def __init__ (self,a,b):  SELF.A = a &NBSP ;  SELF.B = b def __str__ (self): return ' Vector (%d,%d) '% (SE LF.A,SELF.B) def __add__ (self, other): return Vector (SELF.A+OTHER.A , self.b+other.b) v1 = vector (2,8) v2 = vector (4,98) # print V1 + v2v3 = v1 + v2print v3print v1# 3, __del__ () instance extinct Time to execute 4, class Help information the contents of the three quotes __doc__ ' Production of the Class Aattr:method: ' 5, the class instance property is a dictionary, use the following three methods __getitem__ (): Returns the dictionary value of the current instance property __setitem__ (): The key and value in the property are re-assigned __delitem__ (): Delete One of the dictionary key and value values in the instance properties class A (object): ' Production of the class A:attr: method:end. " #__getitem__ #__setitem__#__delitem__class Employee (object): def __init__ (self,name,salary): Self.dict1={} self.dict1[name]=salary def __getitem__ (self, key): if Self.dict1.has_key (key): print Key,self.dict1[key] def __setitem__ (self, key, value): self.dict1[key]=value def _ _delitem__ (self, key): if Self.dict1.has_key (key): del self.dict1[key]& Nbsp;e1=employee (' Lily ', 10000) e1[' Lily ']e1[' Jenny ']=20000e1[' Jenny ']del e1[' Jenny ']e1[' Jenny '] 6, When an instance property is a sequence, the slice Operation __getslice__ (): Slicing the instance Properties __setslice__ (): __delslice__ (): 7, __call__ (Self,*args,**kwargs) By instance, the __call__ method overrides E1 (1,2,3,a= ' abc ')  8, __dict__ return properties, and methods employee.__dict__: Class properties and All methods e.__dict__ instance properties and pointers to classes 9 , __iter__ () uses an iterator that returns the contents of an iterator, calling the __iter__ method, returning an iterator, Def __iter__ (self): return iter (self.list1[:]) Eg: Class Employee (object): def __init__ (self,name,salary): self.list1=[] def __getslice__ (self,i,j): return self.list1[i:j] & nbsp def __setslice__ (self, I, J, sequence): self.list1[i:j]=sequence def __dels lice__ (self, I, j): del self.list1[i,j] def __iter__ (self): &N Bsp return ITER (self.list1[:]) e1=employee (' Lily ', 10000) e1[:4]= ' fjwioefjior ' Print e1[:]for i in E1: Print i10, __new__ (Cls,*args,**kwargs): New When the instance is generated, so is the class method return object.__new__ (Cls,*args,**kwargs) a= A () This time the default is to execute __new__. This built-in method is used in a single example (only one instance) of this design pattern: the idea of a singleton by the __new__ method: Before the instance is created, it is judged whether it was created, if so, not recreated, and no re-creation #__new__ Class A (object): def __init__ (self,a): print ' init method ' &NB SP;SELF.A = a def __new__ (CLS, *args, **kwargs): If not hasattr (CLS, ' _instance '): &N Bsp Cls._instance = object.__new__ (Cls,*args, **kwargs) return cls._instance Whether the instance was created before, if any, re-created, if not, recreated A1 = A (4) print a1.a # 4a2 = A (6) Print a2.a # 6print a1.a # 6 The instance property is the same, but the __init__ () method is executed each time the instance is instantiated
Python Object-oriented: Built-in methods for classes