写个方法去获取一个模块包含模块中的所有公共方法
#coding:utf-8
import keywords
import inspect
func_list = []
def class_loader(cls):
_class = getattr(keywords,cls)
return _class()
def get_kw_list():
for m in keywords.__all__:
_class = class_loader(m)
for name,value in inspect.getmembers(_class):
if not name.startswith("_"):
func_list.append(name)
return func_list
print get_kw_list()
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.youkuaiyun.com/Jasonliujintao/article/details/77449977
反射机制的工厂方法?反射机制的策略模式?
import sys
def func1():
print('func1')
class TestClass():
def p(self):
print('you got me!')
def Main():
className = 'TestClass'
funcName = 'func1'
testClass = globals()[className]()
testClass.p()
testFunc = globals()[funcName]
testFunc()
Main()