是否可以在Python 3中使用类作为字典键?

Tre*_*ent 3 python hash dictionary python-3.x

我正在尝试减少代码中的复制/粘贴,并且偶然发现了这个问题.我已经google了答案但是所有答案都使用类的实例作为键,我找不到任何使用类定义本身作为键(我不知道是否可能).

我的代码是这样的:

# All chunkFuncs keys are class definitions, all values are functions
chunkFuncs = {Math_EXP : Math_EXPChunk, Assignment : AssignmentChunk, Function : FunctionChunk}

def Chunker(chunk, localScope):
    for chunkType in chunkFuncs:
        if isinstance(chunk,chunkType):
            # The next line is where the error is raised
            localScope = chunkFuncs[chunk](chunk,localScope)
            return localScope
Run Code Online (Sandbox Code Playgroud)

而错误就是这个

TypeError: unhashable type: 'Assignment'
Run Code Online (Sandbox Code Playgroud)

以下是类定义:

class Math_EXP(pyPeg.List):
    grammar = [Number,Symbol],pyPeg.maybe_some(Math_OP,[Number,Symbol])

class Assignment(pyPeg.List):
    grammar = Symbol,'=',[Math_EXP,Number]

class Function(pyPeg.List):
    grammar = Symbol,'(',pyPeg.optional(pyPeg.csl([Symbol,Number])),')'
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以用来获得相同的效果?

谢谢.

Tim*_*ers 6

好的,评论已经失控;-)

现在似乎确定类对象不是问题.如果是,则在第一行构建dict时,错误将在第一行触发:

chunkFuncs = {Math_EXP : Math_EXPChunk, Assignment : AssignmentChunk, Function : FunctionChunk}
Run Code Online (Sandbox Code Playgroud)

如果您尝试使用不可用密钥构造dict,则dict创建会立即失败:

>>> {[]: 3}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)

但是你超越了那条线,并且Assignment是你构建的词典的关键.所以错误在这一行:

        localScope = chunkFuncs[chunk](chunk,localScope)
Run Code Online (Sandbox Code Playgroud)

最好的猜测是,它的一个实例Assignment那unhashable:

>>> class mylist(list):
...   pass
...
>>> hash(mylist)
2582159
>>> hash(mylist())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'mylist'
Run Code Online (Sandbox Code Playgroud)

看到? mylist是可以清洗的,但实例mylist()却不是.

后来:最好的猜测是,你无法绕过这个.为什么?由于基类的名称,pyPeg.List.如果它像Python列表那样是可变的,那么实例将不会是可混合的 - 并且不应该是(可变对象总是像dict键一样危险).你仍然可以索引一个字典id(the_instance),但是在语法上是否正确是我无法猜测的,而不需要了解更多关于你的代码.