==UserDict 模塊== ``UserDict`` 模塊包含了一個可繼承的字典類 (事實上是對內建字典類型的 Python 封裝). [Example 2-15 #eg-2-15] 展示了一個增強的字典類, 允許對字典使用 "加/+" 操作並提供了接受關鍵字參數的構造函數. ====Example 2-15. 使用 UserDict 模塊====[eg-2-15] ``` File: userdict-example-1.py import UserDict class FancyDict(UserDict.UserDict): def _ _init_ _(self, data = {}, **kw): UserDict.UserDict._ _init_ _(self) self.update(data) self.update(kw) def _ _add_ _(self, other): dict = FancyDict(self.data) dict.update(b) return dict a = FancyDict(a = 1) b = FancyDict(b = 2) print a + b *B*{'b': 2, 'a': 1}*b* ```
