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 function can be used to retrieve an attribute (such as a method) from an object by name, and the function can be used to check if the attribute is callable (i.e. a method). Here’s an example:

getattr()
callable()
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 with a method . We create an instance of the class and then use reflection to retrieve the method object by name (“my_method”) using . We then check that the attribute is callable using , and if so we invoke the method with an argument (“hello”). The output of this code will be:

MyClass
my_method()
getattr()
callable()
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 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:

globals()
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 . We then use reflection to retrieve the function object by name (“my_function”) using , which returns a dictionary of all the global variables in the current scope. We then check that the attribute is callable using , and if so we invoke the function with an argument (“hello”). The output of this code will be:

my_function()
globals()
callable()

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 as usual. Here’s an example:

getattr()
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 . We then define a class 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 . We then check that the attribute is callable using , and if so we invoke the method with an argument (“hello”). The output of this code will be:

my_method()
MyClass
getattr()
callable()

My method was called with argument: hello

————————

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 function can be used to retrieve an attribute (such as a method) from an object by name, and the function can be used to check if the attribute is callable (i.e. a method). Here’s an example:

getattr()
callable()
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 with a method . We create an instance of the class and then use reflection to retrieve the method object by name (“my_method”) using . We then check that the attribute is callable using , and if so we invoke the method with an argument (“hello”). The output of this code will be:

MyClass
my_method()
getattr()
callable()
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 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:

globals()
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 . We then use reflection to retrieve the function object by name (“my_function”) using , which returns a dictionary of all the global variables in the current scope. We then check that the attribute is callable using , and if so we invoke the function with an argument (“hello”). The output of this code will be:

my_function()
globals()
callable()

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 as usual. Here’s an example:

getattr()
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 . We then define a class 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 . We then check that the attribute is callable using , and if so we invoke the method with an argument (“hello”). The output of this code will be:

my_method()
MyClass
getattr()
callable()

My method was called with argument: hello