python标准库介绍——21 UserDict 模块详解


==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*
```

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM