我想通过程序来检查print是否为Python内置函数。
在Python 3.4.x中,当从Python命令行查询dir(__builtins__)
时,我得到了我要寻找的结果:
['ArithmeticError', 'AssertionError', ..... , 'pow', 'print' ... ]
但是当在.py文件中使用它时:
import sys
def foo:
print(dir(__builtins__))
该调用返回:
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__',
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy',
'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault',
'update', 'values']
我在任何时候都没有重新定义__builtins__
。