Question about inheritence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Catherine Heathcote

    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.
  • Matimus

    #2
    Re: Question about inheritence

    On Jul 22, 9:26 am, Catherine Heathcote
    <catherine.heat hc...@gmail.com wrote:
    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

    Comment

    • Catherine Heathcote

      #3
      Re: Question about inheritence

      On Tue, 22 Jul 2008 09:35:58 -0700, Matimus wrote:
      On Jul 22, 9:26 am, Catherine Heathcote
      <catherine.heat hc...@gmail.com wrote:
      >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.

      Comment

      • Jordan

        #4
        Re: Question about inheritence

        On Jul 22, 12:26 pm, Catherine Heathcote
        <catherine.heat hc...@gmail.com wrote:
        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)

        Comment

        • Fredrik Lundh

          #5
          Re: Question about inheritence

          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(Othe rParent):
          .... def __init__(self, a, b, c):
          .... self.c = c
          .... OtherParent.__i nit__(self, a + b)
          ....

          there's even a common pattern for passing along *all* arguments, no
          matter what they are:
          >>class OtherChild(Othe rParent):
          .... def __init__(self, *args, **kwargs):
          .... # do something here
          .... OtherParent.__i nit__(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>

          Comment

          • Scott David Daniels

            #6
            Re: Question about inheritence

            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
            Scott.Daniels@A cm.Org

            Comment

            • Lawrence D'Oliveiro

              #7
              Re: Question about inheritence

              In message <mailman.490.12 16745662.922.py thon-list@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 "constructo r". It doesn't actually "construct"
              the object, it really "initialize s" it.

              Comment

              Working...