Why no list heritable type?

J

James Stroud

The thread "why not arrays" got me thinking. I would really like to inherit
from a list so that I can add methods based on its contents, say if I filled
it with a type of object and wanted to iterate over all objects. I have built
a wrapper around a list like this for general use:

class list_of_objects:
def __init__(self):
self.data = []
def __len__(self):
return len(self.data)
etc ...

Then it can be heritable and I can add or override methods. Why aren't built
in lists and dictionaries real heritable types that can save this kind of
patchwork? Is there a pythonic reason I am missing here?

James


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/
 
J

Jeff Shannon

James said:
The thread "why not arrays" got me thinking. I would really like to inherit
from a list so that I can add methods based on its contents, say if I filled
it with a type of object and wanted to iterate over all objects. I have built
a wrapper around a list like this for general use:

class list_of_objects:
def __init__(self):
self.data = []
def __len__(self):
return len(self.data)
etc ...

Then it can be heritable and I can add or override methods. Why aren't built
in lists and dictionaries real heritable types that can save this kind of
patchwork? Is there a pythonic reason I am missing here?

James

But they *are* subclassable, since 2.2 at least:

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond ([email protected]) -
see 'Help/About PythonWin' for further copyright information..... pass
....
>>> type(my_list)
>>> l = my_list()
>>> l.append('foo')
>>> l.extend(['bar', 'baz'])
>>> l[2] 'baz'
>>> l ['foo', 'bar', 'baz']
>>> type(l)

Jeff Shannon
Technician/Programmer
Credit International
 
S

Steve Holden

James said:
The thread "why not arrays" got me thinking. I would really like to inherit
from a list so that I can add methods based on its contents, say if I filled
it with a type of object and wanted to iterate over all objects. I have built
a wrapper around a list like this for general use:

class list_of_objects:
def __init__(self):
self.data = []
def __len__(self):
return len(self.data)
etc ...

Then it can be heritable and I can add or override methods. Why aren't built
in lists and dictionaries real heritable types that can save this kind of
patchwork? Is there a pythonic reason I am missing here?

I think the thing you are really missing is the fact that list and the
other built-in types can be used as the basis for inheritance:

Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information. ... def bar(self):
... for item in self:
... print "Bar:", item
...Bar: one
Bar: two
Bar: three
Bar: four
You do need to be somewhat careful, though, to understand the
initialisation mechanism of the new object-based types if you are going
to get the best out of them.

regards
Steve
 
J

Jeremy Bowers

Then it can be heritable and I can add or override methods. Why aren't
built in lists and dictionaries real heritable types that can save this
kind of patchwork? Is there a pythonic reason I am missing here?

Along with others pointing out that list *is* inheritable and we don't
quite know what you mean, I'd point out that any object that inherits the
appropriate "magic methods", as described at
http://python.org/doc/2.4/ref/sequence-types.html
and
http://python.org/doc/2.4/ref/sequence-methods.html
"is" a list for most things you care about.

Want to iterate? Implement __iter__, and "for blah in yourObjType:" will
work as you'd want, even for things like trees if you like. Implement
__getitem__ and you get [] working right.

Python makes matching its "internal protocols" very easy, and you should
definately take advantage of that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top