[Tutor] Making a better list
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sat, 8 Dec 2001 21:57:30 -0800 (PST)
On Sat, 8 Dec 2001, Scott wrote:
> In Python, if I want to make a list-like class (a specialized list of
> x's, say), is it generally better to create a class with a member
> list, or derive a class from the built-in 'list' type (I don't know
> how to do that yet.)
Hi Scott. Yes, it's very possible to derive from the UserList class:
http://www.python.org/doc/lib/module-UserList.html
What we can do is subclass a UserList, and then modify specific behavior,
like how the list pulls elements out. Here's a list of the possible
list-like things that we can redefine:
http://python.org/doc/current/ref/sequence-types.html
For example, perhaps we might want to make a list that capitalizes any
string that we put into it. We could do something like this:
###
>>> class CapitalList(UserList.UserList):
... def __setitem__(self, key, value):
... if type(value) == types.StringType:
... value = string.upper(value)
... self.data[key] = value
>>> l = CapitalList(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[4] = 'hello, this is a test'
>>> l
[0, 1, 2, 3, 'HELLO, THIS IS A TEST', 5, 6, 7, 8, 9]
###
Ok, ok, I admit, this example is completely Useless. *grin* But it shows
that this stuff shouldn't be too difficult to do.
A more serious example might be to make a kind of list that doesn't give
IndexErrors if we try to assign elements beyond the list's size.
###
import UserList
class LooseList(UserList.UserList):
def __setitem__(self, key, value):
if key >= len(self.data):
self.expandList(key - len(self.data) + 1)
self.data[key] = value
def expandList(self, length):
for i in range(length):
self.append(None)
###
Let's see if it works ok:
###
>>> l = LooseList(['hello', 'world'])
>>> l = LooseList(['hello', 'world'])
>>> l[4] = 'divas'
>>> l[42] = 'answer to life'
>>> l
['hello', 'world', None, None, 'divas', None, None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, 'answer to life']
###
As a warning: this is not to say that I encourage the use of this
LooseList: I think it's way too loose. *grin* But it's nice to see that
we have the power to extend Python this way if we wanted.
> HTNTS
>
> (that means 'hope that's not too stupid' :)
That's not a stupid question at all! That was a fun question. I hope
this helps!