473,387 Members | 3,801 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.

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 1383
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
5
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
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...
4
by: jm | last post by:
Consider: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwhenshouldiimplementinterfacesinmycomponent.asp // Code for the IAccount interface module. public...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.