接口
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果
之前看到eval这个函数时一直想不到需要用的地方,今天正在研究python属性相关问题时正好用上了,直接上代码
def func():
f =1
if __name__ == '__main__':
#打印func的内建属性
print dir(func)
输出如下:
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
天啦噜,一个简单的函数就有这么多内建属性,于是我想调用print func.call来看看这些属性究竟是什么鬼
但是这么多属性,不可能一个一个打印的,于是我想遍历这个列表,然后不断执行func.attr,但是遍历出来的attr是字符串啊,eval出场的时候到了
for attr in dir(func):
print attr, ":", eval("func."+attr)
输出如下,可见python私下还是做了很多不知道的东西,有时间好好研究下这些属性
__call__ : <method-wrapper '__call__' of function object at 0x02FBCD30>
__class__ : <type 'function'>
__closure__ : None
__code__ : None
__delattr__ : <method-wrapper '__delattr__' of function object at 0x02FBCD30>
__dict__ : {}
__doc__ : None
__format__ : <built-in method __format__ of function object at 0x02FBCD30>
__get__ : <method-wrapper '__get__' of function object at 0x02FBCD30>
__getattribute__ : <method-wrapper '__getattribute__' of function object at 0x02FBCD30>
__globals__ : {'son': <class __main__.son at 0x02FB9308>, 'uic': <module 'PyQt4.uic' from 'C:\Python27\lib\site-packages\PyQt4\uic\__init__.pyc'>, 'main': <function main at 0x02FBCD70>, 'func': <function func at 0x02FBCD30>, 'QtGui': <module 'PyQt4.QtGui' from 'C:\Python27\lib\site-packages\PyQt4\QtGui.pyd'>, 'parent': <class __main__.parent at 0x02FB92D0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:\\Users\\lht\\Desktop\\\xb1\xcf\xc9\xe8\\test.py', 'Des': <class __main__.Des at 0x02FB9298>, 'qtCreatorFile': 'first.ui', '__package__': None, 'sys': <module 'sys' (built-in)>, 'Ui_MainWindow': <class 'Ui_MainWindow'>, 'MyApp': <class '__main__.MyApp'>, '__name__': '__main__', 'QtBaseClass': <class 'PyQt4.QtGui.QMainWindow'>, 'QtCore': <module 'PyQt4.QtCore' from 'C:\Python27\lib\site-packages\PyQt4\QtCore.pyd'>, '__doc__': None, 'attr': '__globals__'}
__hash__ : <method-wrapper '__hash__' of function object at 0x02FBCD30>
__init__ : <method-wrapper '__init__' of function object at 0x02FBCD30>
__module__ : __main__
__name__ : func
__new__ : <built-in method __new__ of type object at 0x1DB2CF78>
__reduce__ : <built-in method __reduce__ of function object at 0x02FBCD30>
__reduce_ex__ : <built-in method __reduce_ex__ of function object at 0x02FBCD30>
__repr__ : <method-wrapper '__repr__' of function object at 0x02FBCD30>
__setattr__ : <method-wrapper '__setattr__' of function object at 0x02FBCD30>
__sizeof__ : <built-in method __sizeof__ of function object at 0x02FBCD30>
__str__ : <method-wrapper '__str__' of function object at 0x02FBCD30>
__subclasshook__ : <built-in method __subclasshook__ of type object at 0x1DB2CF78>
func_closure : None
func_code : None
func_dict : {}
func_doc : None
func_globals : {'son': <class __main__.son at 0x02FB9308>, 'uic': <module 'PyQt4.uic' from 'C:\Python27\lib\site-packages\PyQt4\uic\__init__.pyc'>, 'main': <function main at 0x02FBCD70>, 'func': <function func at 0x02FBCD30>, 'QtGui': <module 'PyQt4.QtGui' from 'C:\Python27\lib\site-packages\PyQt4\QtGui.pyd'>, 'parent': <class __main__.parent at 0x02FB92D0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:\\Users\\lht\\Desktop\\\xb1\xcf\xc9\xe8\\test.py', 'Des': <class __main__.Des at 0x02FB9298>, 'qtCreatorFile': 'first.ui', '__package__': None, 'sys': <module 'sys' (built-in)>, 'Ui_MainWindow': <class 'Ui_MainWindow'>, 'MyApp': <class '__main__.MyApp'>, '__name__': '__main__', 'QtBaseClass': <class 'PyQt4.QtGui.QMainWindow'>, 'QtCore': <module 'PyQt4.QtCore' from 'C:\Python27\lib\site-packages\PyQt4\QtCore.pyd'>, '__doc__': None, 'attr': 'func_globals'}
func_name : func