REPL - 重新定义后恢复可调用

Mar*_*k N 4 python read-eval-print-loop

我在Python REPL中做了一些工作,我重新定义了一个原始的可调用的.

list = [] 会阻止tuple = list((1,2,3))从工作了.

除了重新启动REPL之外,还有一种方法可以"恢复"或将列表重新分配到其默认值吗?

也许有进口或超级班?或者它会永远丢失并坚持我分配的内容,直到我重新启动REPL?

Pad*_*ham 6

您可以删除该名称 del list

In [9]: list = []    
In [10]: list()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-8b11f83c3293> in <module>()
----> 1 list()

TypeError: 'list' object is not callable    
In [11]: del list    
In [12]: list()
Out[12]: []
Run Code Online (Sandbox Code Playgroud)

或者list = builtins.list对于python3:

In [10]: import builtins
In [11]: list = []    
In [12]: list()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-8b11f83c3293> in <module>()
----> 1 list()

TypeError: 'list' object is not callable    
In [13]: list = builtins.list    
In [14]: list()
Out[14]: []
Run Code Online (Sandbox Code Playgroud)

对于python 2:

In [1]: import __builtin__    
In [2]: list = []    
In [3]: list()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-8b11f83c3293> in <module>()
----> 1 list()    
TypeError: 'list' object is not callable  
In [4]: list = __builtin__.list  
In [5]: list()
Out[5]: []
Run Code Online (Sandbox Code Playgroud)