Search code examples
pythonlistbuilt-in

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?


Solution

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