如果你从dict继承,为什么__dict__总是空的?

时间:2021-03-22 18:11:02

I have a question regarding the subclassing behaviour in Python 2.7.

我对Python 2.7中的子类化行为有疑问。

If I subclass from the built-in dict type, it seems that __ dict __ is always empty. Where does Python save the key/value pairs?

如果我从内置的dict类型继承,似乎__ dict __总是空的。 Python在哪里保存键/值对?

>>> class Foobar(dict):
...     pass
... 
>>> foobar = Foobar()
>>> foobar.__dict__
{}
>>> foobar['test'] = 1
>>> foobar.__dict__
{}
>>> 

5 个解决方案

#1


5  

A partial answer is that you're misunderstanding the purpose of __dict__. __dict__ is used to store attributes, not items, and it's present in most user-defined objects. Indeed, if you subclass dict in the appropriate way, __dict__ will have values in it.

部分答案是你误解了__dict__的目的。 __dict__用于存储属性,而不是项目,它存在于大多数用户定义的对象中。实际上,如果你以适当的方式继承dict,__ dict__将包含值。

>>> class Foo(dict):
...     def __init__(self, *args, **kwargs):
...         super(Foo, self).__init__(*args, **kwargs)
...         self.banana = 'banana'
...         self['banana'] = 'not really a banana'
... 
>>> f = Foo()
>>> f.__dict__
{'banana': 'banana'}
>>> f.banana
'banana'
>>> f['banana']
'not really a banana'

#2


6  

__dict__ is where an object's attributes are stored. Dicts' items are not attributes, they are items. Most dicts have no data attributes.

__dict__是存储对象属性的位置。 Dicts的项目不是属性,它们是项目。大多数dicts没有数据属性。

BTW: subclassing dict is difficult to do properly, depending what you are trying to achieve. For example, you can override its __setitem__ method, but then update won't use it.

BTW:根据你想要实现的目标,很难正确地进行子类化dict。例如,您可以覆盖其__setitem__方法,但随后更新将不会使用它。

#3


5  

The dict class is implemented purely in C as a built-in. Its data storage is private to that implementation.

dict类纯粹在C中作为内置实现。其数据存储对于该实现是私有的。

As a thought experiment, imagine if it put the name/value pairs into a Python dict, how would that dict store them? In another Python dict? And then, well, you get the idea!

作为一个思想实验,想象一下如果它将名称/值对放入Python dict中,那dict将如何存储它们?在另一个Python词典?然后,好吧,你明白了!

#4


2  

Because a dict object doesn't actually have __dict__ so the __dict__ you are referencing is the dict local to your object Foobar. Because Foobar has no attributes __dict__ is empty.

因为dict对象实际上没有__dict__所以你引用的__dict__是你的对象Foobar本地的dict。因为Foobar没有属性__dict__是空的。

#5


1  

__dict__ is for attributes:

__dict__用于属性:

>>> class D(dict):
    pass

>>> d=D()
>>> d.__dict__
{}
>>> d.x=5
>>> d.__dict__
{'x': 5}

#1


5  

A partial answer is that you're misunderstanding the purpose of __dict__. __dict__ is used to store attributes, not items, and it's present in most user-defined objects. Indeed, if you subclass dict in the appropriate way, __dict__ will have values in it.

部分答案是你误解了__dict__的目的。 __dict__用于存储属性,而不是项目,它存在于大多数用户定义的对象中。实际上,如果你以适当的方式继承dict,__ dict__将包含值。

>>> class Foo(dict):
...     def __init__(self, *args, **kwargs):
...         super(Foo, self).__init__(*args, **kwargs)
...         self.banana = 'banana'
...         self['banana'] = 'not really a banana'
... 
>>> f = Foo()
>>> f.__dict__
{'banana': 'banana'}
>>> f.banana
'banana'
>>> f['banana']
'not really a banana'

#2


6  

__dict__ is where an object's attributes are stored. Dicts' items are not attributes, they are items. Most dicts have no data attributes.

__dict__是存储对象属性的位置。 Dicts的项目不是属性,它们是项目。大多数dicts没有数据属性。

BTW: subclassing dict is difficult to do properly, depending what you are trying to achieve. For example, you can override its __setitem__ method, but then update won't use it.

BTW:根据你想要实现的目标,很难正确地进行子类化dict。例如,您可以覆盖其__setitem__方法,但随后更新将不会使用它。

#3


5  

The dict class is implemented purely in C as a built-in. Its data storage is private to that implementation.

dict类纯粹在C中作为内置实现。其数据存储对于该实现是私有的。

As a thought experiment, imagine if it put the name/value pairs into a Python dict, how would that dict store them? In another Python dict? And then, well, you get the idea!

作为一个思想实验,想象一下如果它将名称/值对放入Python dict中,那dict将如何存储它们?在另一个Python词典?然后,好吧,你明白了!

#4


2  

Because a dict object doesn't actually have __dict__ so the __dict__ you are referencing is the dict local to your object Foobar. Because Foobar has no attributes __dict__ is empty.

因为dict对象实际上没有__dict__所以你引用的__dict__是你的对象Foobar本地的dict。因为Foobar没有属性__dict__是空的。

#5


1  

__dict__ is for attributes:

__dict__用于属性:

>>> class D(dict):
    pass

>>> d=D()
>>> d.__dict__
{}
>>> d.x=5
>>> d.__dict__
{'x': 5}