会java的开发人员都知道java中动态调用一个类中的方法都是使用反射机制,动态加载class(包名+类名+'.class')获取类对象,然后再获取method,再调用对应方法等
但是python中在一个py文件中,不通过from...import来引入另一个py,然后调用其中的函数呢?
废话不多说,直接上代码:
mytest1.py:
class Test1: def test1(self, name): print("this is my_test1.py class Test1 func is test1 args is %s" % name) def test2(self, name): print("this is my_test2.py class Test1 func is test2 args is %s" % name) def test2(name): print("this is my_test1.py func is test2 args is %s" % name)
我想在test_main.py中调用my_test1.py中的test2方法,并传递name参数
test_main.py
#使用__import__ ip_module = __import__('my_test1') print(dir(ip_module))#查看my_test1中有哪些属性、类、函数 #调用引入py文件中的类中的函数 test2_func = getattr(ip_module, "test2")#获取test2函数 test2_func('test2_name')#调用并传递参数['__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'test2']
这样就可以调用啦
但是,如果我在my_test1.py中Test1类中的test1()方法呢?如下:
ip_module = __import__('my_test1') test1_class = getattr(ip_module, "Test1") test1_obj = test1_class() test1_func = getattr(test1_obj, 'test1') test1_func("test1_name")
运行结果:
this is my_test1.py class Test1 func is test1 args is test1_name
当然,我也可以循环遍历Test1下的所有方法:
for attr in dir(test1_obj): if attr[0] != '_': class_attr_obj = getattr(test1_obj, attr) if hasattr(class_attr_obj, '__call__'): class_attr_obj('aa') else: print(attr, ' type:', type(class_attr_obj), ' value:', class_attr_obj)
这样会把Test1下的所有方法都调用一遍
this is my_test1.py class Test1 func is test1 args is aa
this is my_test2.py class Test1 func is test2 args is aa
另外还可以用另一种方式,需要依赖 importlib
import importlib my_test1_module = importlib.import_module('my_test1', ".") my_test1_func_test2 = getattr(my_test1_module,"test2") my_test1_func_test2("my_test1_func_test2")#调用my_test1中的test2方法 非Test1类中的 my_test1_cls = getattr(my_test1_module, "Test1") cls_obj = my_test1_cls() my_test1_test1 = getattr(cls_obj, 'test1') my_test1_test1('my_test1_test1')#调用Test1中的test1方法 my_test1_test2 = getattr(cls_obj, 'test2') my_test1_test2('my_test1_test2')#调用Test1中的test2方法
运行:
this is my_test1.py func is test2 args is my_test1_func_test2
this is my_test1.py class Test1 func is test1 args is my_test1_test1
this is my_test2.py class Test1 func is test2 args is my_test1_test2
如果我在test_1包下创建了my_test2.py,代码如下:
class Test2: def test1(self, name): print("this is test_1.my_test2.py class Test2 func is test1 args is %s" % name) def test2(self, name): print("this is test_1.my_test2.py class Test2 func is test2 args is %s" % name) def test2(name): print("this is test_1.my_test2.py func is test2 args is %s" % name)
那么,我想在test_main.py下调用my_test2该如何调用呢?
其实很简单,只需要加载的时候加上包名就可以了
import importlib my_test1_module = importlib.import_module('test_1.my_test2', ".") my_test1_func_test2 = getattr(my_test1_module,"test2") my_test1_func_test2("my_test1_func_test2")#调用my_test1中的test2方法 非Test1类中的 my_test1_cls = getattr(my_test1_module, "Test2") cls_obj = my_test1_cls() my_test1_test1 = getattr(cls_obj, 'test1') my_test1_test1('my_test1_test1')#调用Test1中的test1方法 my_test1_test2 = getattr(cls_obj, 'test2') my_test1_test2('my_test1_test2')#调用Test1中的test2方法
结果如下:
this is test_1.my_test2.py func is test2 args is my_test1_func_test2
this is test_1.my_test2.py class Test2 func is test1 args is my_test1_test1
this is test_1.my_test2.py class Test2 func is test2 args is my_test1_test2
以上代码均都经过自己测试运行,如果有问题请留言。大家一起学习,一起进步