Merge pull request #18 from gaufung/master
modify the magic method which extend the dict becomes a multi treemaster
commit
ae2e1b02b7
|
@ -22,25 +22,29 @@ class People(object):
|
||||||
print("\t".join([str(item) for item in sorted([People("abc", 18), People("abe", 19), People("abe", 12), People("abc", 17)])]))
|
print("\t".join([str(item) for item in sorted([People("abc", 18), People("abe", 19), People("abe", 12), People("abc", 17)])]))
|
||||||
|
|
||||||
|
|
||||||
# Python实现任意深度的赋值 例如a[0] = 'value1'; a[1][2] = 'value2'; a[3][4][5] = 'value3'
|
# Python实现任意深度的赋值 例如a[0] = 'value1'; a[0][2] = 'value2'; a[0][2][3] = 'value3',构造出一颗多叉树类似结构
|
||||||
class MyDict(dict):
|
class MyDict(dict):
|
||||||
|
def __init__(self,name=''): # 增加初始化函数,将需要放置的value存储到内部中
|
||||||
|
self.name=name
|
||||||
|
def __str__(self): # 增加 __str__ 函数,将存储的元素输出出来
|
||||||
|
return self.name
|
||||||
def __setitem__(self, key, value): # 该函数不做任何改动 这里只是为了输出
|
def __setitem__(self, key, value): # 该函数不做任何改动 这里只是为了输出
|
||||||
print("setitem:", key, value, self)
|
print("setitem:", key, value, self)
|
||||||
super().__setitem__(key, value)
|
temp = MyDict(value)
|
||||||
|
super(MyDict,self).__setitem__(key, temp)
|
||||||
return
|
return
|
||||||
|
|
||||||
def __getitem__(self, item): # 主要技巧在该函数
|
def __getitem__(self, item): # 主要技巧在该函数
|
||||||
print("getitem:", item, self)
|
print("getitem:", item, self)
|
||||||
# 基本思路: a[1][2]赋值时 需要先取出a[1] 然后给a[1]的[2]赋值
|
# 基本思路: a[1][2]赋值时 需要先取出a[1] 然后给a[1]的[2]赋值
|
||||||
if item not in self: # 如果a[1]不存在
|
if item not in self: # 如果a[1]不存在
|
||||||
temp = MyDict() # 则需要新建一个dict
|
temp = MyDict(item) # 则需要新建一个dict
|
||||||
super().__setitem__(item, temp) # 并使得a[1] = dict
|
super(MyDict,self).__setitem__(item, temp) # 并使得a[1] = dict
|
||||||
return temp # 返回a[1] 使得a[1][2] = value有效
|
return temp # 返回a[1] 使得a[1][2] = value有效
|
||||||
return super().__getitem__(item) # 如果a[1]存在 则直接返回a[1]
|
return super(MyDict,self).__getitem__(item) # 如果a[1]存在 则直接返回a[1]
|
||||||
|
|
||||||
# 使用例子:
|
# 使用例子:
|
||||||
test = MyDict()
|
test = MyDict()
|
||||||
test[0] = 'test'
|
test[0] = 'test'
|
||||||
test[1][2] = 'test1'
|
test[0][2] = 'test1'
|
||||||
test[3][4][5] = 'test2'
|
test[0][2][5] = 'test2'
|
||||||
|
|
Loading…
Reference in New Issue