gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:29:43 25 4
gpt4 key购买 nike

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

list = []将阻止 tuple = list((1,2,3)) 继续工作。

除了重新启动 REPL 之外,是否有办法“恢复”或将列表重新分配为其默认值?

也许有导入或父类(super 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 - 重新定义后恢复可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30946341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com