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