与内置功能的命名冲突 [英] Naming conflict with built-in function

查看:52
本文介绍了与内置功能的命名冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我犯了如下错误:

>>> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆