Why does this work:
class Bunch(dict):
def __init__(self, *args, **kwargs):
super(Bunch, self).__init__(*args, **kwargs)
self.__dict__ = self
b = Bunch()
b["a"] = 1
print(b.a)
Albeit with a circular reference:
import sys
print(sys.getrefcount(b))
# ref count is 3 when normally it is 2
But not this:
class Bunch(dict):
@property
def __dict__(self):
return self
b = Bunch()
b["a"] = 1
b.a
# raises AttributeError
While __dict__
is an attribute by most outward appearances, there's some special behavior being implemented behind the scenes which is bypassing the descriptor logic created by the property
.