python 动态调用模块&类&方法
2013-08-28 12:24
661 查看
转载自:/article/5084685.html
一直想知道python里有没有类似php中的 $classname->$method() 或 call_user_func
今天有时间查了一下,示例代码如下:
classname.py
test.py
?
一直想知道python里有没有类似php中的 $classname->$method() 或 call_user_func
今天有时间查了一下,示例代码如下:
classname.py
1 #!/usr/bin/python 2 3 class classname: 4 def mod1(self): 5 pass 6 7 def echo(self): 8 print "test"
test.py
1 #!/usr/bin/python 2 3 def test(): 4 clsname = "classname" 5 method = "echo" 6 7 obj = __import__(clsname) # import module 8 c = getattr(obj,clsname) 9 obj = c() # new class 10 #print(obj) 11 #obj.echo() 12 mtd = getattr(obj,method) 13 mtd() # call def 14 15 if __name__ == '__main__': 16 test()
?
相关文章推荐
- [python]python 动态调用模块&类&方法
- python 动态调用模块、类、方法(django项目)
- python利用字典保存配置实现动态调用模块类方法
- Python动态导入模块的方法实例分析
- Python学习之模块间互相调用方法详解
- python下面通过ctypes模块调用c++库的方法
- python下面通过ctypes模块调用c++库的方法
- 分配模块、通配符和动态方法调用
- python动态加载模块及变量是否存在方法
- Python实现动态加载模块、类、函数的方法分析
- python下面通过ctypes模块调用c++库的方法
- S1:动态方法调用:call & apply
- python下面通过ctypes模块调用c++库的方法
- 在Python中通过threading模块定义和调用线程的方法
- 根据字符串名称动态调用Python的函数和对象方法
- 不安装python运行python文件或者是用其他语言调用python模块的方法
- C#调用Python模块的方法
- python中动态加载模块和类方法实现
- python下面通过ctypes模块调用c++库的方法
- python技巧31[Python 动态加载模块的3种方法]