By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,773 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,773 IT Pros & Developers. It's quick & easy.

Question about inheritence

P: n/a
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
Share this Question
Share on Google+
6 Replies

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.