REPL - 重新定义后恢复可调用
REPL - recover callable after redefining it
我在 Python REPL 中做了一些工作,我重新定义了一个原始可调用对象。
即list = []
将阻止 tuple = list((1,2,3))
继续工作。
除了重启 REPL 之外,有没有办法 'recover' 或将列表重新分配为其默认值?
也许有导入或超级class?或者在我重新启动 REPL 之前,它会永远丢失并坚持我分配给它的内容吗?
你可以删除名字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]: []
或 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]: []
对于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]: []
我在 Python REPL 中做了一些工作,我重新定义了一个原始可调用对象。
即list = []
将阻止 tuple = list((1,2,3))
继续工作。
除了重启 REPL 之外,有没有办法 'recover' 或将列表重新分配为其默认值?
也许有导入或超级class?或者在我重新启动 REPL 之前,它会永远丢失并坚持我分配给它的内容吗?
你可以删除名字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]: []
或 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]: []
对于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]: []