[Tutor] Making a better list
dman
dsh8290@rit.edu
Sun, 9 Dec 2001 08:11:54 -0500
On Sat, Dec 08, 2001 at 09:57:30PM -0800, Danny Yoo wrote:
| 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
Or, if you have 2.2, you can subclass the built-in 'list'.
class MyList( list ) :
pass
I think a good rule-of-thumb for deciding whether to use inheritance
or composition is :
o how much is your new type supposed to behave like the existing
type?
o how much of the existing functionality do you want to keep
as-is?
If you only want to change a little bit of the functionality,
inheritance would be easier, but composition is still easy enough with
the __getattr__ and __setattr__ methods.
HTH,
-D
--
Contrary to popular belief, Unix is user friendly.
It just happens to be selective about who it makes friends with.
-- Dave Parnas