尝试在 python 中输入 object 时出错
Error when trying to get object type in python
我有一个主脚本,它导入了另一个我一直在写的 python 库。
该库包含以下命令:
print getattr(__builtins__, "list")
当我从库中调用它时会产生以下错误:
'dict' object has no attribute 'list'
但是,如果我将相同的命令复制并粘贴到主脚本中,它就可以正常工作。知道为什么这不起作用吗?
我的主文件的 header 看起来像这样:
#/usr/bin/env python
from sys import argv
import re, sys, os, argparse
sys.path.extend(map(os.path.abspath, ['C:/Users/xxxx/scripts/modules/']))
import general
我的 "general" 图书馆的 header 是:
#!/usr/bin/env python
from sys import argv
import re, sys, os, argparse
def getBuiltin(name):
##Convert a string into an attribute
try:
return getattr(__builtins__, name)
except Exception as e:
print "Unhandled type in function \"get_builtin\""
print name
print e
exit()
我是这样调用图书馆的:
print general.getBuiltin("list")
其中,"getBuiltin" 是我的函数的名称
你也可以查看这个问题:Python: What's the difference between __builtin__ and __builtins__?
正如您在 akent answer 中看到的,builtins 在主模块和另一个模块中是不同的:
Straight from the python documentation:
http://docs.python.org/reference/executionmodel.html
By default, when in the main module, builtins is the built-in
module builtin (note: no 's'); when in any other module,
builtins is an alias for the dictionary of the builtin module itself.
builtins can be set to a user-created dictionary to create a weak form of restricted execution.
CPython implementation detail: Users should not touch builtins; it
is strictly an implementation detail. Users wanting to override values
in the builtins namespace should import the builtin (no 's')
module and modify its attributes appropriately. The namespace for a
module is automatically created the first time a module is imported.
Note that in Python3, the module builtin has been renamed to
builtins to avoid some of this confusion.
我有一个主脚本,它导入了另一个我一直在写的 python 库。 该库包含以下命令:
print getattr(__builtins__, "list")
当我从库中调用它时会产生以下错误:
'dict' object has no attribute 'list'
但是,如果我将相同的命令复制并粘贴到主脚本中,它就可以正常工作。知道为什么这不起作用吗?
我的主文件的 header 看起来像这样:
#/usr/bin/env python
from sys import argv
import re, sys, os, argparse
sys.path.extend(map(os.path.abspath, ['C:/Users/xxxx/scripts/modules/']))
import general
我的 "general" 图书馆的 header 是:
#!/usr/bin/env python
from sys import argv
import re, sys, os, argparse
def getBuiltin(name):
##Convert a string into an attribute
try:
return getattr(__builtins__, name)
except Exception as e:
print "Unhandled type in function \"get_builtin\""
print name
print e
exit()
我是这样调用图书馆的:
print general.getBuiltin("list")
其中,"getBuiltin" 是我的函数的名称
你也可以查看这个问题:Python: What's the difference between __builtin__ and __builtins__?
正如您在 akent answer 中看到的,builtins 在主模块和另一个模块中是不同的:
Straight from the python documentation: http://docs.python.org/reference/executionmodel.html
By default, when in the main module, builtins is the built-in module builtin (note: no 's'); when in any other module, builtins is an alias for the dictionary of the builtin module itself.
builtins can be set to a user-created dictionary to create a weak form of restricted execution.
CPython implementation detail: Users should not touch builtins; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should import the builtin (no 's') module and modify its attributes appropriately. The namespace for a module is automatically created the first time a module is imported. Note that in Python3, the module builtin has been renamed to builtins to avoid some of this confusion.