命名与内置函数冲突 [英] Naming conflict with built-in function

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

问题描述

我犯了以下错误:

<预><代码>>>>列表 = ['a', 'b', 'c']

但现在我想使用内置函数list().如您所见,listname list 和内置函数list() 之间存在命名冲突.

如何在不重新启动 Python shell 的情况下将 list 用作内置函数而不是变量?

解决方案

使用 __builtins__.list__builtins__['__list__'](取决于上下文),或者干脆再次删除list(del list).

无需导入:

<预><代码>>>>__builtins__.list<输入列表">

__builtins__ 的存在是一个 CPython 实现细节;在 __main__ 模块中它是一个模块,在其他地方它是模块 __dict__ 字典.Jython、IronPython 和 PyPy 可能会选择根本不提供此功能.将 __builtin__ 模块用于这些平台,或用于Python 3 兼容实现,builtins 模块:

<预><代码>>>>导入 __builtin__>>>__builtin__.list<输入列表">

I have made a mistake as below:

>>> list = ['a', 'b', 'c']

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().

How can I use list as a built-in function not the variable without restarting the Python shell?

解决方案

Use __builtins__.list or __builtins__['__list__'] (depending on context), or simply delete list again (del list).

No imports needed:

>>> __builtins__.list
<type 'list'>

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天全站免登陆