为什么当我点击 Dictionary 变量时 Spyder 不能打开它?

Why can't Spyder open the Dictionary variable when I click on it?

我通过以下class创建我的字典,最后我认为这是一个普通的字典子class。

class my_dictionary(dict):
    # Function to add key:value 
    def add(self, key, value): 
        self[key] = value 

我使用这个字典 subclass 将列表与字符串相关联:

from collections import OrderedDict

timeDict = my_dictionary()

filename = 'name'
t = [0, 1, 2]

timeDict.add(filename, t)

代码创建字典并按预期使用它:

但是,我无法通过在变量资源管理器中单击来检查 Spyder 中的词典!我收到以下错误对话框:

Error

Spyder was unable to retrieve the value of this variable from the console.

The error message was:

AttributeError("Can't get attribute 'my_dictionary' on ",)

这是 spyder 中的一个已知问题:#8856。据开发人员称,Spyder 4.0 的修复程序正在开发中。

与此同时,您的 use-case 有一个解决方法。 add 方法不添加任何新功能,因此您可以使用普通的 dict。如果您选择这样做,语句 timeDict.add(filename, t) 可以替换为 timeDict[filename] = t

如果您的目标是尝试覆盖构建它 类,您现在将无法使用 Spyder 的变量浏览器。