与内置功能的命名冲突 [英] Naming conflict with built-in function
问题描述
我犯了如下错误:
>>> list = ['a', 'b', 'c']
但是现在我想使用内置函数list()
.如您所见,列表名称list
与内置函数list()
之间存在命名冲突.
But now I want to use the built-in function list()
. As you can see, there is a naming conflict between listname list
and the built-in function list()
.
如何在不重新启动Python shell的情况下将list
用作内置函数而不是变量?
How can I use list
as a built-in function not the variable without restarting the Python shell?
推荐答案
使用__builtins__.list
或__builtins__['__list__']
(取决于上下文),或再次删除list
(del list
).
Use __builtins__.list
or __builtins__['__list__']
(depending on context), or simply delete list
again (del list
).
无需导入:
>>> __builtins__.list
<type 'list'>
__builtins__
的存在是CPython实现的详细信息;在__main__
模块中,它是一个模块,在其他任何地方,它都是模块__dict__
字典. Jython,IronPython和PyPy可能选择完全不提供此功能.将 __builtin__
模块用于这些平台或与Python 3兼容的实现, builtins
模块:
The presence of __builtins__
is a CPython implementation detail; in the __main__
module it is a module, everywhere else it is the module __dict__
dictionary. Jython, IronPython and PyPy may opt to not make this available at all. Use the __builtin__
module for those platforms, or for Python 3 compatible implementations, the builtins
module:
>>> import __builtin__
>>> __builtin__.list
<type 'list'>
这篇关于与内置功能的命名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!