469,288 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

Basic Inheritance Question

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.
Jul 18 '05 #1
4 1263
Matthew Bell wrote:
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.

I think this is what you want:
class MyList(list): def __init__(self,seq=[1,2,3]):
super(MyList,self).__init__(seq)

a=MyList()
a [1, 2, 3] b=MyList([4,5])
b [4, 5] a + b [1, 2, 3, 4, 5]


Jul 18 '05 #2
Matthew Bell wrote:
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.


If I got it right, you should have:

class A:
def __init__(self):
self.data=[]

class B(A):
def __init__(self):
A.__init__(self)
self.data=[1,2,3]

cheers
Jul 18 '05 #3
Matthew Bell schreef:
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.

You were nearly there. You don't need a <magictoken>;
__init__ does not return anything.
Just initialize the baseclass with the things you want:
class myList(list): def __init__(self):
list.__init__(self, [1,2,3])

m = myList()
m [1, 2, 3]


Regards,

Ruud

Jul 18 '05 #4
Ruud de Jong wrote:
Matthew Bell schreef:
I've got a conceptual problem to do with inheritance.
I'd be grateful if someone could help to clear up my
confusion.
<...deletia...>
You were nearly there. You don't need a <magictoken>;
__init__ does not return anything.
Just initialize the baseclass with the things you want:

>>> class myList(list): def __init__(self):
list.__init__(self, [1,2,3])


>>> m = myList()
>>> m [1, 2, 3] >>>


Ruud,

The light has come on! Thanks very, very much for the
pointer. Given Python's general elegance I knew there
must be an obvious way of doing this but I just couldn't
see it. I do now.

Thanks again,
Matthew.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by gouqizi.lvcha | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
4 posts views Thread by MikeB | last post: by
7 posts views Thread by jason | last post: by
14 posts views Thread by MartinRinehart | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.