Python __call__() method

        This section then introduces   a very special instance method in Python classes, namely __call__() . The function of this method is similar to overloading the () operator in the class , so that the class instance object can be used in the form of " object name () " just like calling a normal function .
for example:

class CLanguage:
    # 定义__call__方法
    def __call__(self,name,add):
        print("调用__call__()方法",name,add)

clangs = CLanguage()
clangs("C语言中文网","http://c.biancheng.net")

The program execution result is:

Call __call__() method C language Chinese website http://c.biancheng.net

It can be seen that by implementing the __call__() method in the CLanguage class, the clangs instance object becomes a callable object.

In Python, any object that can apply () directly to itself and execute it is called a callable object . Callable objects include custom functions , Python built-in functions , and class instance objects described in this section .

        For callable objects, actually "name()" can be understood as shorthand for "name.__call__()". Still taking the clangs instance object defined in the above program as an example, the last line of code can also be rewritten as follows:

clangs.__call__("C语言中文网","http://c.biancheng.net")

Running the program will find that its running results are exactly the same as before.
Here is another example of a custom function, for example:

def say():
    print("Python教程:http://c.biancheng.net/python")

say()
say.__call__()

The program execution result is:

Python教程:http://c.biancheng.net/python
Python教程:http://c.biancheng.net/python

Not only that, but the instance methods in the class also have the above two calling methods, no examples are given here, interested readers can write their own codes to try.

Use __call__() to make up for the short board of hasattr() function

        The previous chapter introduced the usage of the hasattr() function. The function of this function is to find out whether the instance object of the class contains the attribute or method of the specified name, but this function has a defect, that is, it cannot judge whether the specified name is a class or not . Properties are also class methods.
        To solve this problem, we can use the concept of callable objects. You should know that the methods contained in the class instance object are actually callable objects, but the class attributes are not . for example:

class CLanguage:
    def __init__ (self):
        self.name = "C语言中文网"
        self.add = "http://c.biancheng.net"
    def say(self):
        print("我正在学Python")

clangs = CLanguage()
if hasattr(clangs,"name"):
    print(hasattr(clangs.name,"__call__"))
print("**********")
if hasattr(clangs,"say"):
    print(hasattr(clangs.say,"__call__"))

The program execution result is:

False
**********
True

It can be seen that since name is a class attribute, it does not have a __call__() method named __call__; and say is a class method, which is a callable object, so it has a __call__() method .

Guess you like

Origin blog.csdn.net/liuqinhou/article/details/129877368