Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call the function by the _ _ call__ method in python

Shulou Source: shulou.com Published: 2022-06-03 17:36:10 10月04日 Update

Xiaobian to share with you how to implement the function call in python__call__method, I hope you have gained something after reading this article, let's discuss it together!

description

1. When calling a function decorated with overload, the__call__method in the Function class will be called.

To get the__call__method to get the correct function from the get function in the namespace and call it.

examples

def __call__(self, *args, **kwargs): """Overwrite the__call__method that makes an instance of a class callable"" #Get the function to be called from the virtual namespace fn = Namespace.get_instance().get(self.fn, *args) if not fn: raise Exception("no matching function found. ") #Call the wrapped function and return the result of the call return fn(*args, **kwargs)

This method gets the correct function from the virtual namespace. If no function is found, it's an Exception. If found, it calls the function and returns the call result.

After reading this article, I believe you have a certain understanding of "how to implement the function call in python__call_method". If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!

Tags: Functions methods spaces instances articles results parameters done objects more knowledge industry information channels channels adjustable encapsulated Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Shulou Tech Info Shulou Technology MariaDB