The school began to enter the laboratory, need to assist big bro work, mainly in OpenStack code parsing, but involves a lot of Python advanced usage, a bit of trouble at the same time, while doing the project slowly update the blog. This time first write about the use of __call__, because often see it but do not know exactly what to do.
Discovering that __call__ actually overloads a class with a "()", which means that a class can be called like a function, saying it might not be clear, directly on the example.
#/usr/bin/env pythonclassTest:def __init__(self,a): SELF.A=adef __call__(self,b): C= self.a+bPrintCdefdisplay (self):Printself.atest= Test ("This is test!") Test.display () Test ("# #Append Something")
Output result: This is test!
This is test!# #Append something
As you can see, the __init__ function initializes the value in self as if it were a constructor, assigns a to "This is test!", and then we invoke the instance name directly, and here is the role of __call__ ().
As for why to do so, let me slowly discover it.
Python __call__ function and usage of built-in functions