Basic Inheritance Question
Matthew Bell
usenet at spling.plus.com
Fri Mar 19 06:06:47 EST 2004
I've got a conceptual problem to do with inheritance.
I'd be grateful if someone could help to clear up my
confusion.
An example. Say I need a class that's basically a
list, with all the normal list methods, but I want a
custom __init__ so that the list that is created is
[1,2,3] rather than [] (yes, it's a bogus example,
but it does to make the point). Without bothering
with inheritance, I could do:
class mysimplelistclass:
def __init__(self):
self.internallist = [1, 2, 3]
This would work but I would, of course, need to define
methods in "mysimpleclass" to deal with all the various
methods that the original list class provides.
Obviously, the thing to do is to inherit from the list
class, override the __init__ method and leave the rest
of the normal list class's methods untouched. So I'd
write something like:
class myinheritedlistclass(list):
def __init__(self):
<now what?>
It's at this point I get confused. Obviously, I don't
use the "self.internallist = [1, 2, 3]" bit as before
because I'd then need to override all of the rest of
the normal list methods to get them to act on
self.internallist.
Conceptually, I suppose I need something like:
<somemagictoken> = superclass.self.__init__([1, 2, 3)]
but that is, of course, totally ridiculous.
Essentially, then, if I've inherited another class, how
do I create an instance of the class I've inherited such
that methods I haven't overrriden will still work, and
how can I then refer to that instance from my subclass?
I can guess it's something to do with "self" but exactly
what, I'm really at a loss.
Any assistance in my confusion would be gratefully received!
Regards,
Matthew.
More information about the Python-list
mailing list