A built-in method, Adv.
Method dual:
Built-in functions / special syntax / grammar sugar / built-in module
1. __call__ corresponding to the object () method call __call__ [Flask (Django)]
A () () # class name () (), call __call__ method
is equivalent to instantiate an object to get in on the subject () ==> and as the result of the above, the method is equivalent to calling __call__
2. __new__ particularly important to open up memory space class constructor has return
1. open space, belonging to the object obj = object .__ new __ (cls ) # Create a space (memory address)
2. spatial objects passed to self, performs the init
3. the object of this space returned to the caller
3. __len__ len (obj) calls __len__ this method has return
built-in methods built-in functions and classes are linked
len (obj) this method is equivalent to calling the __len__ of obj
__len__ method return the return value is the value len function
if no object obj __len__ a method, then len function error
4. __str__ str (obj), ' % s'% obj, print (obj) has return
Print an object corresponding to an object calling a method __str__
built-in data types, the built-in type, equivalent to the implementation __str__ Print ( str (Object))
__str__: str (obj), the requirement must be achieved obj __str__, this method requires the return value must be a string str type
Print% S
5. the method for initializing the __init__ special syntax, the instantiated __new_ after calling _
6. __repr__: __str__ spare tire is, if there __str__ method, then print% s str
are __str__ first implementation method, and using the return value __str__
If not __str__, then print% s str will perform the repr
the repr (obj), R & lt%
in __str__ subclasses, subclasses __str__ go first, to find if there is no up, not just the parent class Object, the parent class implementation __str__
However, if in addition to outside the parent object are not __str__, on the implementation of __repr__ subclass, if there is no sub-class, but also
the up keep looking __repr__ method of the parent class.
Could not find, print a certain object memory address
__repr __, __ str__
__repr__ repr% r
__str__ Print% S str
Without str, all it had to be called __str__ method syntax will call __repr__
turn, if not repr, repr will use the parent class, not a substitute for str
in succession: If the subclass does not str, str will find first-class parent, find subclass repr - extend
all the double-under method, there is no need for you to call in outside
but there are always some other special syntax for these dual built-in functions under way to automatically trigger
1. __call__ method
class Apple(): def __call__(self, *args, **kwargs): print('__call__方法 ...') class Banana(Apple): def __init__(self,call): self.call = Call () # here corresponds to instantiate an object of class self.call () # by self.call (an object) is instantiated class to get a plus (), is performed __call__ method A = the Apple () A () # object A plus () method performs __call__, or Apple plus the class name () () method is executed __call__ B = Banana (Apple) # pass a class name
2. __new__ and __init__ method
class the Apple: DEF __new__ (CLS, * args, ** kwargs): # in the implementation of new space did not create space, can only turn the class obj = Object. __new__ (CLS) # address self object, obj Print ( " in new method in " , obj) return obj DEF __init__ (Self): Print ( " init method in " , Self) obj = Apple() print(obj) # Singleton class (more important) # If the class, from start to finish only one instance, then this class is a singleton class class the Apple: __apple = None DEF __new__ (CLS, * args, ** kwargs): IF Not CLS. __apple : # determines whether there CLS. __apple = Object. __new__ is (CLS) # object address return CLS. __apple DEF the __init__ (Self): Pass a1 = Apple() a2 = Apple() print(a1) print(a2) # Explained: once by the Apple () object instances a1, perform the method __new__, # determines cls .__ apple == None :, it is not present, to perform the following # code of the object address a1, by _ method _new__ passed cls .__ apple, # so cls .__ apple, to get the object address a1, a1 cls .__ apple == # second object is instantiated a2, __new__ method executed, it is determined that there cls .__ apple , # is a target address a1, it is not performed if the inside contents directly to the target a1 # address returned to the a2, so a2 = a1, the memory address is a singleton class. # General is the beginning of a set of attributes does not exist, the first object is determined by the address # passed to it, while the target address is returned to the original object, a second instantiation found # by determining which property values exist, and not to perform, and which present value (that is, return to # the first address of the instance of the object) to the second object,
3. __len__ method and __iter__
class Len: DEF __init__ (Self): Pass DEF __len__ (Self): Print ( " the implementation of the __len__ " ) return len (Self. __dict__ ) # returns the object attribute much len mean length DEF __iter__ (Self) : Print ( ' execution method __iter__ ... ' ) return len (Self. the __dict__ ) a = Len () len (A) # len (Object) This method performs len Print (len (A)) # call __len__ methods, and receiving a return value # len (obj) of this method is equivalent to calling the obj __len__ of # __len_ _ len method return value is the function's return value # if no object obj __len__ a method, then len function will error a. __iter__ () # equivalent __iter__ method called Print (a. __iter__ ()) # __iter__ method call, and receive a return value