473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 mysimplelistcla ss:
def __init__(self):
self.internalli st = [1, 2, 3]

This would work but I would, of course, need to define
methods in "mysimplecl ass" 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 myinheritedlist class(list):
def __init__(self):
<now what?>

It's at this point I get confused. Obviously, I don't
use the "self.internall ist = [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.internalli st.

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 1408
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 mysimplelistcla ss:
def __init__(self):
self.internalli st = [1, 2, 3]

This would work but I would, of course, need to define
methods in "mysimplecl ass" 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 myinheritedlist class(list):
def __init__(self):
<now what?>

It's at this point I get confused. Obviously, I don't
use the "self.internall ist = [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.internalli st.

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,s eq=[1,2,3]):
super(MyList,se lf).__init__(se q)

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 mysimplelistcla ss:
def __init__(self):
self.internalli st = [1, 2, 3]

This would work but I would, of course, need to define
methods in "mysimplecl ass" 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 myinheritedlist class(list):
def __init__(self):
<now what?>

It's at this point I get confused. Obviously, I don't
use the "self.internall ist = [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.internalli st.

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__(s elf, [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__(s elf, [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
3753
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 application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
5
1623
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
23386
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 examples?
4
8583
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 interface IAccount { void PostInterest(); void DeductFees(IFeeSchedule feeSchedule); }
45
6368
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
2102
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 so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
4
1732
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, - Programming Visual Basic by Balena (MS Press) and - Visual Basic 2005 by Willis (WROX), but they don't go into the forms design aspects and describing the various controls at all. What bookscan I get that will cover that?
7
4475
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
14
1852
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 logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start, stop ):
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.