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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章