[python]python 动态调用模块&类&方法
2012-05-11 17:33
761 查看
一直想知道python里有没有类似php中的 $classname->$method() 或 call_user_func
今天有时间查了一下,示例代码如下:
classname.py
test.py
今天有时间查了一下,示例代码如下:
classname.py
#!/usr/bin/python class classname: def mod1(self): pass def echo(self): print "test"
test.py
#!/usr/bin/python def test(): clsname = "classname" method = "echo" obj = __import__(clsname) # import module c = getattr(obj,clsname) obj = c() # new class #print(obj) #obj.echo() mtd = getattr(obj,method) mtd() # call def if __name__ == '__main__': test()
/usr/bin/python test.py
相关文章推荐
- python 动态调用模块&类&方法
- python 动态调用模块、类、方法(django项目)
- python利用字典保存配置实现动态调用模块类方法
- 在Python中通过threading模块定义和调用线程的方法
- python下面通过ctypes模块调用c++库的方法
- python调用模块&函数
- python下面通过ctypes模块调用c++库的方法
- Python 动态加载模块的3种方法
- python文件中的__name__=='__main__'的使用及调用其他py文件中的函数方法
- python下面通过ctypes模块调用c++库的方法
- python调用C模块的方法
- python调用自编模块的方法
- python下面通过ctypes模块调用c++库的方法
- python调用java模块SmartXLS和jpype修改excel文件的方法
- Python动态加载模块的3种方法
- C++ 调用Python3 脚本中无法引入内建模块的问题 解决方法
- python下面通过ctypes模块调用c++库的方法
- python下面通过ctypes模块调用c++库的方法
- 在Python中通过threading模块定义和调用线程的方法
- python 动态调用模块内的函数