473,466 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

calling objects parents' __init__?

Normally, when I inherit from a class that I have written,
I want instances of the new class to call the __init__ of
the old (actually, starting from the top of the tree, and
moving on down).

What is the preferred way to do this?

Thanks,

Tobiah

Jul 18 '05 #1
2 3269

<us**@domain.invalid> wrote in message
news:e1******************************@news.teranew s.com...
Normally, when I inherit from a class that I have written,
I want instances of the new class to call the __init__ of
the old (actually, starting from the top of the tree, and
moving on down).

What is the preferred way to do this?
If you're using a new style class (one that inherits from
object,) look at the super() built in function. Be a bit
cautious with it, the usage isn't exactly obvious.
There's also an article on the method resolution order
somewhere that makes quite interesting reading.

If you're using an old style class, you have to call
them directly: superklas.__init__(self).

John Roth

Thanks,

Tobiah

Jul 18 '05 #2
us**@domain.invalid wrote:

Normally, when I inherit from a class that I have written,
I want instances of the new class to call the __init__ of
the old (actually, starting from the top of the tree, and
moving on down).

What is the preferred way to do this?


The preferred way is to call your parent's __init__, if it
has one:

class Parent:
def __init__(self):
# useful stuff happens here

class Child:
def __init__(self):
Parent.__init__(self)
# other stuff, some useful, happens here

There are more sophisticated, less readable approaches, and I suspect
Python 2.2 or 2.3 add a new idiom as well.

-Peter
Jul 18 '05 #3

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
7
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section...
4
by: Martin Maney | last post by:
I've been to the cookbook (both forms, since they're sometimes usefully different), but that gave rise to more questions than answers. Heck, let me pull some of them up while I'm at it. ...
8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
6
by: Rob Cowie | last post by:
Hi all, Is there a simple way to call every method of an object from its __init__()? For example, given the following class, what would I replace the comment line in __init__() with to result...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
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
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
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...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.