Python __call__ methodAn Introduction to Magic MethodsMethod Names having double underscores as prefixes and suffixes are the reserved methods for particular use in Python. For example, the __init__ method is used for object constructors or the __call__ method for making object callable. We call these methods dunder methods, where dunder refers to Double Under (Underscores). These dunder methods are also known as magic methods – However, there is nothing magical related to them. There are many developers in the Python community who are not a big fan of the word ‘magic’, as it delivers a feeling that the use of this method is discouraged, but quite the contradiction is factual. Understanding the callable functionThe Object in Python is called callable when that object is defined within the __call__() function. The function for the same can be defined as x(arg 1, arg 2, …), which is short for x.__call__(arg1, arg2, …). Note: The callable() method returns the Boolean value for whether the object appears to be callable. This function returns True if the object is callable; else, it returns False. Moreover, it is also possible that this function may return True even when the object is not callable. Still, if this method returns False, then the object is not callable.Moreover, a Python class is always Callable. Thus, we can always utilize the callable() function with an object of the class, not the class itself. Let us consider the following example in order to understand the behavior of the Python callable() function. Example: Output: Employee Class is callable = True Employee object is callable = False Explanation: In the above example, we have defined a class Employee that takes a variable n = 0. We have then defined a function and instantiated the class. At last, we have checked whether the class and its object are callable or not using the callable() function. Let us consider an example of __call__() function. Example: Output: <__main__.Person object at 0x000002706DF41FD0> The Person object is callable: True Explanation: In the above example, we have defined the Person class and the variables person_id = 0 and person_name = ” “. We have then defined the initializing function along with the callable function. At last, we have instantiated the class, printed the object, and checked whether the object is callable or not. And as a result, the person object appears to be callable. Moreover, we can observe that we have used the *args to allow passing variable arguments and **kwargs to allow passing named arguments to the __call__() method. Now, let us consider another example where we will have used the callable() function to check whether the object is callable or not and then called the object as a function. Example: Output: Printing Arguments Printing Keyword Arguments Printing Arguments 15 30 Printing Keyword Arguments Printing Arguments 15 30 Printing Keyword Arguments Printing Arguments 15 30 {'a': 4, 'b': 8} Printing Keyword Arguments Printing Arguments 15 B Printing Keyword Arguments personname == George personid == 50 Explanation: In the above example, we have defined an if-statement where if the object m is callable, then the object is called as a function without arguments, with only arguments, with __call__() function, with the arguments of different types and with arguments and keyword arguments. As a result, the required objects have been called successfully. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263527.html