Naming conflict with built-in function

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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.

Method 4

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([])

Method 5

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

Method 6

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]

Method 7

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x