Python中通过反射来调用方法

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中通过反射来调用方法相关的知识,希望对你有一定的参考价值。

Is there a way to pass invoke function by method name in string, which means call the method by reflection

Yes, you can use reflection in Python to invoke a method by passing its name as a string. The built-in getattr() function can be used to retrieve an attribute (such as a method) from an object by name, and the callable() function can be used to check if the attribute is callable (i.e. a method). Here\'s an example:

class MyClass:
    def my_method(self, arg):
        print("My method was called with argument:", arg)

# create an instance of MyClass
obj = MyClass()

# invoke the method by passing its name as a string
method_name = "my_method"
if hasattr(obj, method_name) and callable(getattr(obj, method_name)):
    method = getattr(obj, method_name)
    method("hello")

In this example, we define a class MyClass with a method my_method(). We create an instance of the class and then use reflection to retrieve the method object by name ("my_method") using getattr(). We then check that the attribute is callable using callable(), and if so we invoke the method with an argument ("hello"). The output of this code will be:

My method was called with argument: hello

 

What if I don\'t have a class name? Is there a default class name in python file only have methods in it? Is it possible to use method = getattr(obj, method_name)

If you don\'t have a class name and just want to call a function by name using reflection, you can use the built-in globals() function to get a dictionary of all the global variables in the current scope. This dictionary will contain any functions defined at the top level of the module. Here\'s an example:

def my_function(arg):
    print("My function was called with argument:", arg)

# invoke the function by passing its name as a string
function_name = "my_function"
if function_name in globals() and callable(globals()[function_name]):
    function = globals()[function_name]
    function("hello")

In this example, we define a function my_function(). We then use reflection to retrieve the function object by name ("my_function") using globals(), which returns a dictionary of all the global variables in the current scope. We then check that the attribute is callable using callable(), and if so we invoke the function with an argument ("hello"). The output of this code will be:

My function was called with argument: hello

Regarding your second question, if you only have methods defined in a module and no class, you can define a class with a default name (e.g. "MyClass") and add the methods as instance methods. Then you can use reflection to retrieve the method object by name using getattr() as usual. Here\'s an example:

def my_method(arg):
    print("My method was called with argument:", arg)

# define a class with a default name and add the method as an instance method
class MyClass:
    my_method = my_method

# create an instance of the class
obj = MyClass()

# invoke the method by passing its name as a string
method_name = "my_method"
if hasattr(obj, method_name) and callable(getattr(obj, method_name)):
    method = getattr(obj, method_name)
    method("hello")

In this example, we define a method my_method(). We then define a class MyClass with a default name and add the method as an instance method. We create an instance of the class and then use reflection to retrieve the method object by name ("my_method") using getattr(). We then check that the attribute is callable using callable(), and if so we invoke the method with an argument ("hello"). The output of this code will be:

My method was called with argument: hello

 

以上是关于Python中通过反射来调用方法的主要内容,如果未能解决你的问题,请参考以下文章

如何在python中通过引用调用静态方法[重复]

Java中通过反射获取带参数类型的方法

Java中通过method对象来调用方法

C#中通过委托来调用对象方法的基本过程

反射,元类

在 Python 中通过套接字发送大量数据的正确方法是啥?