python标准库介绍——22 UserList 模块详解


==UserList 模块==


``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装).

在 [Example 2-16 #eg-2-16] 中, //AutoList// 实例类似一个普通的列表对象, 但它允许你通过赋值为列表添加项目.

====Example 2-16. 使用 UserList 模块====[eg-2-16]

```
File: userlist-example-1.py

import UserList

class AutoList(UserList.UserList):

    def _ _setitem_ _(self, i, item):
        if i == len(self.data):
            self.data.append(item)
        else:
            self.data[i] = item

list = AutoList()

for i in range(10):
    list[i] = i

print list

*B*[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]*b*
```

 


免责声明!

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



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