我有一些令人费解的代码,我正试图迁移到Typescript。看看这个:
def add_Octopus(self, code, cracker, fate, description, arm_number, ink_content, fate_pointer, churlishness=None):
self.special_octopoda[
Octopus(code, description, arm_number, fate, churlishness, self, ink_content, fate_pointer)] = fate_pointer, cracker在我看来,八达通对象被用作名为special_octopoda的字典中的关键字。这在Python中是允许的吗?它肯定不是Typescript/Javascript。
发布于 2018-12-13 22:15:28
是的,您可以使用任何不可变对象作为Python字典中的键。
Octopus类必须以某种方式创建不可变的实例。例如,它可以是元组的子类,也可以使用__slots__来实现。
发布于 2018-12-13 22:16:55
只有不可变类型才能用作python中的键。对于可能的解决方法,请将其包装为元组:
self.special_octopoda(<something>, Octopus(...))或者让你的类变得可哈希:
class Octopus:
def __hash__(self):
# hash implementationhttps://stackoverflow.com/questions/53763823
复制相似问题