Naming conflict with built-in function
Step one: rebind the list to a different name
lst = list
Step two: delete the list
variable
del list
Step three: don't do it again
I prefer this over __builtins__.list
simply because it saves the typing, and you aren't still left with a variable named list
. However, it is always best to avoid the problem altogether. When writing production code, always remember not to have variables named the same as built in functions.
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'>
Do not use built-in functions, or types as variable names. It is just as simple as that, the language is not meant for that. And it makes no sense to do so.
Not only that - but using the name "list" for a list is very ambiguous, and I doubt it is even remotely usable in any real code.
There are a few reasons why you should NOT ever shadow a built-in. Some of the more serious ones are below:
- Compatibility, the code would not work with other modules.
- Confusion, anyone reading your code will not understand what is going on.
- Circumstances, many of the built-ins use other built-ins, changing one can have unexpected results on other aspects of code.