The UserList Module
The UserList module contains a list class that can be subclassed (simply a Python wrapper for the built-in list type).
In Example 2-16, AutoList instances work just like ordinary lists, except that they allow you to insert items at the end by assigning to them.
Example 2-16. Using the UserList Module
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
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Категории