Question about inheritence

  • Thread starter Catherine Heathcote
  • Start date
C

Catherine Heathcote

If I create a new class inherited from another with a constructor, what
happens with the new class's constructer?
Thanks for your time.
 
M

Matimus

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
 
C

Catherine Heathcote

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.
 
J

Jordan

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)
 
F

Fredrik Lundh

Catherine said:
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.
.... def __init__(self):
.... print "init parent"
........ pass
....init parent
.... def __init__(self):
.... print "init 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:
.... def __init__(self):
.... Parent.__init__(self)
.... print "init 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):
.... 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:
.... 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:
.... def __init__(self):
.... print "init parent"
........ def __init__(self):
.... super(Child, self).__init__()
.... print "init child"
....init parent
init child

hope this helps!

</F>
 
L

Lawrence D'Oliveiro

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top