python 反射
如何在python中实现 类似java中,
Class.forName().newInstance()的功能
1.静态导入
tommy@ubuntu:~$ python Python 2.7.2+ (default, Oct 4 2011, 20:03:08) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from time import time >>> globals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'time': <built-in function time>, '__doc__': None, '__package__': None} >>> globals()["time"] <built-in function time> >>>
如果不显示导入fromtimeimporttime,会报异常,
tommy@ubuntu:~$ python Python 2.7.2+ (default, Oct 4 2011, 20:03:08) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> globals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None} >>> globals()["time"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'time' >>>
2.动态导入
tommy@ubuntu:~$ python Python 2.7.2+ (default, Oct 4 2011, 20:03:08) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> module=__import__('time') >>> module <module 'time' (built-in)> >>> se_time=getattr(module, "time") >>> se_time <built-in function time> >>> se_time() 1332210676.294227 >>>
name=self.__class__.__name__
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16