Skip to content

Commit f8cb6b2

Browse files
author
liuxinwei
committed
为 Bunch.merge 提供3层以上嵌套支持
1 parent 3b82823 commit f8cb6b2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/tvm_book/utils.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ class Bunch(dict):
22
def __init__(self, *args, **kwargs):
33
super().__init__(*args, **kwargs)
44
self.__dict__ = self # 这意味着 Bunch 类的实例将具有与字典相同的行为,可以使用点符号访问和修改其键值对
5+
self._convert_nested_dicts()
6+
7+
def _convert_nested_dicts(self):
58
for k, v in self.__dict__.items():
69
if isinstance(v, dict):
7-
self.__dict__[k] = Bunch(**v) # 支持嵌套结构
8-
10+
self.__dict__[k] = Bunch(**v) # 将字典转换为 Bunch 对象
11+
elif isinstance(v, Bunch):
12+
v._convert_nested_dicts() # 递归处理嵌套的 Bunch 对象
13+
914
def merge(self, other):
1015
"""提供递归合并功能"""
16+
other = Bunch(**other)
1117
for k, v in other.items():
1218
if k not in self:
1319
self[k] = other[k]

0 commit comments

Comments
 (0)