18
votes

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?

7
nice question, we should link to this whenever people use a builtin name - jamylak
Worth noting: probably the best thing for you to do is to use one of the methods below to access the built-in version, and rebind the list name to it, to undo your mistake. list = __builtins__.list - Henry Keiter

7 Answers

29
votes

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'>
32
votes

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.

9
votes

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.
4
votes

To repair the mistake, there is another option - if we remember that the built-in 'function' list() is actually a class, then we can just do this:

list = type([])
2
votes

use __builtin__.list in py2x:

>>> import __builtin__ 
>>> __builtin__.list
<type 'list'>

Don't use __builtins__.list :

From the docs:

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.

for py3x:

>>> import builtins
>>> builtins.list
<class 'list'>
1
votes

It is always available as __builtins__.list:

>>> __builtins__.list
<class 'list'>
>>> list = [1, 2, 3]
>>> __builtins__.list
<class 'list'>

If you happen to rebind that, however, you're out of options.

You can also use the module __builtin__ (or builtins, without the underscores, in Python 3) but you have to import it. But these are different ways to spell the same thing, rather than being an extra option - modifying one affects both:

>>> import builtins
>>> builtins.list
<class 'list'>
>>> builtins.list = [1, 2, 3]
>>> builtins.list
[1, 2, 3]
>>> __builtins__.list
[1, 2, 3]
0
votes

Yes, others are saying above, don't use the name of a builtin as a variable name. This goes for list, dict, etc.

Likewise, as others have said, you have access to the type list through __builtins__.list. So if you need to call list, you can still find it, as long as you haven't rebound __builtins__.list also.

Importantly, though, list is a name. You've rebound it to an instance of a list. If you want list to mean <type 'list'> again, just rebind it again. In Python 2.7:

>>> __builtins__.list
<type 'list'>
>>> list
<type 'list'>
>>> list = [1, 2, 3]
>>> list
[1, 2, 3]
>>> fred = list
>>> fred
[1, 2, 3]
>>> list = __builtins__.list
>>> list
<type 'list'>