473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

list inheritance or delegation?

[python 2.3.3, x86 linux]
I need an object that behaves much like a list, but has some
additional features. It seems that I can do this either by
inheriting from type list, or by emulating list by defining
__len__, __getitem__, and maybe a bunch of other methods. Thirdly,
I could inherit from UserList, but that's deprecated now.

Inheriting from list would save me implementing a bunch of methods
that I might want. But if I inherit from list, how do I get access
to the data? E.g., I need a customized 'setitem' method that does
the insert and then does some other things too. With UserList, I could:

class mylist(UserList):
def __setitem__(self,i,y):
self.dostuff(y)
self.data[i]=y

but when inheriting from 'list', I'm not sure how to do this.

I couldn't find anything in the docs about inheriting from builtin
types, though it seems to be an encouraged practice. It seems like if
one is encouraged to inherit from builtins, there needs to be at least
a concise description of the 'inheritable api' for each such type.
E.g. does __init__ or __new__ handle initial values. Maybe I'm just
missing something obvious...

So, I'm asking:
1) what are the issues/tradoffs between inheriting from list and emulating
it with my own methods referencing a list data member.

2) how do I get enough access to list's data to do the inheritance.

3) I'm whining about lack of documentation for #2.

Thanks,
George Young
Jul 18 '05 #1
1 1731
On Tue, 16 Mar 2004 12:51:15 -0800, george young wrote:
[python 2.3.3, x86 linux]
I need an object that behaves much like a list, but has some additional
features. It seems that I can do this either by inheriting from type
list, or by emulating list by defining __len__, __getitem__, and maybe a
bunch of other methods. Thirdly, I could inherit from UserList, but
that's deprecated now.

Inheriting from list would save me implementing a bunch of methods that I
might want. But if I inherit from list, how do I get access to the data?
E.g., I need a customized 'setitem' method that does the insert and then
does some other things too. With UserList, I could:

class mylist(UserList):
def __setitem__(self,i,y):
self.dostuff(y)
self.data[i]=y

but when inheriting from 'list', I'm not sure how to do this.

class mylist(list):
def __setitem__(self,i,y):
self.dostuff(y)
list.__setitem__(self,i,y)

same with __init__ etc.
I couldn't find anything in the docs about inheriting from builtin types,
though it seems to be an encouraged practice. It seems like if one is
encouraged to inherit from builtins, there needs to be at least a concise
description of the 'inheritable api' for each such type. E.g. does
__init__ or __new__ handle initial values. Maybe I'm just missing
something obvious...

So, I'm asking:
1) what are the issues/tradoffs between inheriting from list and emulating
it with my own methods referencing a list data member.
One drawback is you (may) have to override
all list methods, rather than leave them undefined. The slicing stuff is particularly hairy.

Simon.

2) how do I get enough access to list's data to do the inheritance.

3) I'm whining about lack of documentation for #2.

Thanks,
George Young


Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Allen | last post by:
Hi all, I'm still having a hard time with my derived class. I know I make reference to some MS-specific tech. but the question should be independent of that; I'm having a hard time with the...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
0
by: rossabri | last post by:
This topic has been addressed in limited detail in other threads: "sockets don't play nice with new style classes :(" May 14 2005....
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
8
by: Anand | last post by:
I am trying to implement some kind of object inheritance. Just like one class can extend from another, I want to do the same on objects dynamically. I just thought that I can share my excitement...
3
by: tonialbrown | last post by:
I am having some problems with an update statement. The problem is the data comes from a list box lstDelFrom that the user selects for the required record that they are copying the data from. This...
5
by: Mike Kent | last post by:
For Python 2.5 and new-style classes, what special method is called for mylist = seq and for del mylist (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.