python __getitem__, __setitem__ 实现属性的索引式存取
class MyDictionary(object):
"""docstring for MyDictionary"""
kv = {}
def __init__(self):
super(MyDictionary, self).__init__()
def __getitem__(self, key):
return self.kv[key]
def __setitem__(self, key, value):
self.kv[key] = value
md = MyDictionary()
# 像操作字典一样操作属性,这样就可以实现动态的属性存取了,屌了
# 当然一般的直接使用"."操作就搞定了
md['name'] = 'mark'
print('{0}'.format(md['name']))