我有一个类似字典的对象,其中存储描述符:
class MyDict(object):
def __init__(self):
dict.__init__(self)
def __getitem__(self, key):
v = dict.__getitem__(self, key)
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v
class MyDescriptor(object):
def __init__(self, value, attrib={}):
self.__value = value
self.attrib= attrib
def __get__(self, instance, owner):
return self.__value
def __set__(self, instance, value):
self.__value = value
我希望能够做到以下几点:
d = MyDict()
d['1'] = MyDescriptor("123", {"name": "val"})
print d['1'] # prints "123"
print d['1'].attrib["name"] # prints "val"
我的课不工作。请你帮助我好吗?