Python: 意外地为 repr 分配了一个 int 值

Python: Accidentally assigned repr an int value

我不太清楚我是怎么想到的,但在某个时候我输入了

repr = 64

进入 spyder 的 python 控制台。当我现在尝试 运行 repr(b64) 时,会发生这种情况:

repr(b64)
Traceback (most recent call last):
  File "<ipython-input-23-8c64b01419a6>", line 1, in <module>
    repr(b64)
TypeError: 'int' object is not callable

我可以在不重启 spyder 的情况下解决这个问题吗?

使用builtins.repr:

>>> repr = 42
>>> repr(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> from builtins import repr
>>> repr(42)
'42'

(或按照 user2357112 的建议使用 del)。

删除你的变量:

del repr

这将清除您创建的绑定,取消隐藏内置。 (它不会删除内置的 repr。)这会让您回到比 repr = builtins.repr 稍微干净的状态,尽管这通常无关紧要。