为什么Python中的dict类需要过度填充
我正在阅读a的源代码,类为什么Python中的dict类需要过度填充,python,class,oop,inheritance,dictionary,Python,Class,Oop,Inheritance,Dictionary,我正在阅读a的源代码,类bank的定义如下: class Bank(object): """ the bank class contains all the bank operations """ def __init__(self, name): """ instantiate the class """ self.name = str(name) self.customers = Customers() class Cust
bank
的定义如下:
class Bank(object):
""" the bank class contains all the bank operations """
def __init__(self, name):
""" instantiate the class """
self.name = str(name)
self.customers = Customers()
class Customers(dict):
""" the customers class extends the dictionary object """
def __setitem__(self, key, item):
self.__dict__[key] = item
def __getitem__(self, key):
return self.__dict__[key]
def __repr__(self):
return repr(self.__dict__)
def __len__(self):
return len(self.__dict__)
def __delitem__(self, key):
del self.__dict__[key]
def keys(self):
return self.__dict__.keys()
def values(self):
return self.__dict__.values()
def __cmp__(self, dict):
return cmp(self.__dict__, dict)
def __contains__(self, item):
return item in self.__dict__
def add(self, key, value):
self.__dict__[key] = value
def __iter__(self):
return iter(self.__dict__)
def __call__(self):
return self.__dict__
def __unicode__(self):
return unicode(repr(self.__dict__))
现在,self.customers
是customers
类的另一个实例,定义如下:
class Bank(object):
""" the bank class contains all the bank operations """
def __init__(self, name):
""" instantiate the class """
self.name = str(name)
self.customers = Customers()
class Customers(dict):
""" the customers class extends the dictionary object """
def __setitem__(self, key, item):
self.__dict__[key] = item
def __getitem__(self, key):
return self.__dict__[key]
def __repr__(self):
return repr(self.__dict__)
def __len__(self):
return len(self.__dict__)
def __delitem__(self, key):
del self.__dict__[key]
def keys(self):
return self.__dict__.keys()
def values(self):
return self.__dict__.values()
def __cmp__(self, dict):
return cmp(self.__dict__, dict)
def __contains__(self, item):
return item in self.__dict__
def add(self, key, value):
self.__dict__[key] = value
def __iter__(self):
return iter(self.__dict__)
def __call__(self):
return self.__dict__
def __unicode__(self):
return unicode(repr(self.__dict__))
- 根据我的理解,当添加新功能或其行为与以前的功能不同时,我们将覆盖功能。为什么我们在
类中覆盖Customer
的功能。我们不能简单地使用dict
?因为我们没有在这里添加任何新内容self.customers=dict()
- 这门课不仅仅是一个
字典
;它还支持键的属性访问,因为它将所有字典访问权委托给实例\uuuuu dict\uuuu
属性,该属性包含所有属性
演示:
用一本普通字典是做不到的。您找到的实现是这个问题答案的一个相当复杂的版本:@python:自定义类的实例存储您在
\uuuu dict\uuu
属性中为它们设置的属性。dict
的这个子类将所有对键的访问重定向到该\uuuuu dict\uuuuu
属性,因此设置键可以有效地设置属性。不,不是真的,请参见链接的问题。这个类的实现方式,它也不需要继承自dict
。您已经做出了一个有问题的假设,即支持属性访问是一个设计目标,而不是作者不知道他们在做什么的巧合。此代码来自的项目实际上不使用属性访问,尽管没有覆盖它们,但它最终混合了对\uuu dict\uuuu
属性和继承的dict结构的访问,并且它没有找到它添加的任何条目。@user2357112:对,鉴于所呈现的类是子类化dict
,但实际上没有正确地这样做,我并不感到惊讶,会犯更多的错误。孤立地说,这看起来像是试图制作一个支持属性访问的字典。这段代码很可怕,毫无意义,而且有缺陷。它实际上是两个dict,self
和self.\uuuu dict\uuuu
,因为只有一些dict方法被重写,所以一些方法看不到其他方法操作的效果。例如,它们没有被覆盖,因此在添加客户后无法找到客户<代码>\uuuu repr\uuuuu有误导性,\uuuu call\uuuuuu
很奇怪,而且总体而言,这只是一个糟糕的代码。你能帮我改进一下吗?我还在用Python编写一个银行应用程序,非常感谢您的帮助。请完全忽略此代码。对于dict,只需使用常规dict,而无需将其子类化。对于银行代码的其余部分,独立于此代码所做的工作,做出您自己的设计决策。这段代码不是一个有用的起点。