使用similar approach to a previous question about subclassing a float class不会在字典中产生任何键。例如:
class Foo(dict):
def __new__(cls, value, extra):
return super().__new__(cls, value)
def __init__(self, value, extra):
dict.__init__(value)
self.extra = extra
运行Foo({'a':1}, 1).keys()
返回空的字典键dict_keys([])
。
如何在Python中使用额外的参数正确地将字典子类化?
答案 0 :(得分:1)