from collections import UserList
# same with X(list):
class X(UserList):
def method(self):
print("It runs")
xx = X()
xx.method()
print(hash(xx.method))
# same with list's own methods:
# hash(xx.append) # also causes TypeError
Python {3.8的原因TypeError: unhashable type: 'X'
关于如何进行这项工作的任何想法? (除了升级解释器)
答案 0 :(得分:0)
解决方法是在初始化中使用lambda(还有更好的选择吗?)。 最初,它必须从pyee库中将数据收集到类似列表的对象中。并将列表继承对象的方法用作事件回调。
下面是一个示例,以阐明事情的工作原理:
from pyee import BaseEventEmitter
ee = BaseEventEmitter()
class Data(list):
def __init__(self):
super().__init__()
self.store = lambda d: self.append(d) # (!)workaround for py<3.8
storage = Data()
storage.store("some data") # test
print(hash(storage.store)) # This works in python 3.7
ee.on("received", storage.store) # here we need it to be hashable
# ee.on("received", storage.append) # and this fails in python 3.7
#... some even emitting code