473,396 Members | 1,895 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,396 software developers and data experts.

Question about inheritence

If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Thanks for your time.
Jul 22 '08 #1
6 846
On Jul 22, 9:26*am, Catherine Heathcote
<catherine.heathc...@gmail.comwrote:
If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Thanks for your time.
Nothing, unless you call it in your constructor.

class Base(object):
def __init__(self):
print "Base constructor called"

# without calling the base class constructor
class C(Base):
def __init__(self):
print "C constructor called"

# call the base class constructor using super
class D(Base):
def __init__(self):
super(D, self).__init__()
print "D constructor called"

c = C()
d = D()
Matt
Jul 22 '08 #2
On Tue, 22 Jul 2008 09:35:58 -0700, Matimus wrote:
On Jul 22, 9:26*am, Catherine Heathcote
<catherine.heathc...@gmail.comwrote:
>If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Thanks for your time.

Nothing, unless you call it in your constructor.

class Base(object):
def __init__(self):
print "Base constructor called"

# without calling the base class constructor
class C(Base):
def __init__(self):
print "C constructor called"

# call the base class constructor using super
class D(Base):
def __init__(self):
super(D, self).__init__()
print "D constructor called"

c = C()
d = D()
Matt
Aha! Makes sence, thankyou. As you can probably tell I am new to Python,
but not programming as a whole.
Jul 22 '08 #3
On Jul 22, 12:26*pm, Catherine Heathcote
<catherine.heathc...@gmail.comwrote:
If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Thanks for your time.
Well, the __init__ method of the subclass is called, and from within
it you can call the superclass constructor.

Here is a sample code:

class A():
def __init__(self, bla):
#do some stuff here

class B(A):
def __init__(self, bla2):
#do some stuff here
A.__init__(self,bla)
Jul 22 '08 #4
Catherine Heathcote wrote:
If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
assuming that you mean "is it called or not?":

Python doesn't really have constructors; when you create an object,
Python first creates the object and then calls the __init__ method, if
available

that method is an ordinary method, and behaves like all other methods:
if you override it, your version is used. if you don't, the other one
is used.
>>class Parent:
.... def __init__(self):
.... print "init parent"
....
>>class Child(Parent):
.... pass
....
>>o = Child()
init parent
>>class Child(Parent):
.... def __init__(self):
.... print "init child"
....
>>o = Child()
init child

if you want to override the parent's init method, but still use its
functionality, you can add an explicit call to the method:
>>class Child(Parent):
.... def __init__(self):
.... Parent.__init__(self)
.... print "init child"
....
>>o = Child()
init parent
init child

since it's an ordinary method call, you don't have to initialize the
parent the first thing you do. you can also pass in any arguments you
want (including arguments passed to the child constructor):
>>class OtherChild(OtherParent):
.... def __init__(self, a, b, c):
.... self.c = c
.... OtherParent.__init__(self, a + b)
....

there's even a common pattern for passing along *all* arguments, no
matter what they are:
>>class OtherChild(OtherParent):
.... def __init__(self, *args, **kwargs):
.... # do something here
.... OtherParent.__init__(self, *args, **kwargs)
.... # do something else here
....

instead of explicitly naming the baseclass, you can use the "super"
method to automatically look up the parent class, but this only works
if the parent class inherits from "object" or a subclass thereof:
>>class Parent(object):
.... def __init__(self):
.... print "init parent"
....
>>class Child(Parent):
.... def __init__(self):
.... super(Child, self).__init__()
.... print "init child"
....
>>o = Child()
init parent
init child

hope this helps!

</F>

Jul 22 '08 #5
Fredrik Lundh wrote:
Catherine Heathcote wrote:
>If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Python doesn't really have constructors; when you create an object,
Python first creates the object and then calls the __init__ method, if
available
To elaborate a bit on Fredrik's response, there is a sense in which
Python has constructors, but, to the extent it does, a constructor is
the __new__, __init__ pair. For immutables, everything happens in
__new__, for mutables, most things happen in the __init__ chain.

--Scott David Daniels
Sc***********@Acm.Org
Jul 23 '08 #6
In message <ma************************************@python.org >, Fredrik
Lundh wrote:
Python doesn't really have constructors; when you create an object,
Python first creates the object and then calls the __init__ method, if
available
That's the usual meaning of "constructor". It doesn't actually "construct"
the object, it really "initializes" it.
Jul 25 '08 #7

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

Similar topics

17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
10
by: Frank Millman | last post by:
Hi all I recently posted a question about subclassing. I did not explain my full requirement very clearly, and my proposed solution was not pretty. I will attempt to explain what I am trying to...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
3
by: Filimon Roukoutakis | last post by:
Dear all, assuming that through a mechanism, for example reflexion, the Derived** is known explicitly. Would it be legal (and "moral") to do this conversion by a cast (probably reinterpret would...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
0
by: tirumalab | last post by:
Hi all, im working on C#, in my current proj we're using one form as the base form and deriving it from all the forms ,its working fine with windows applicatons.But we want to develop the same...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
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,...

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.