Passing arguments to subclasses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Dann

    Passing arguments to subclasses

    May I ask a simple newbie question, which I presume is true, but for
    which I can't readily find confirmation:

    Let's say I have a parent class with an __init__ method explicitly
    defined:

    class ParentClass(obj ect):
    def __init__(self, keyword1, keyword2):
    etc

    and I subclass this:

    class ChildClass(Pare ntClass):
    # No __init__ method explicitly defined

    Now I presume that I can instantiate a child object as:

    child = ChildClass(arg1 , arg2)

    and arg1, arg2 will be passed through to the 'constructor' of the
    antecedent ParentClass (there being no overrriding __init__ method
    defined for ChildClass) and mapping to keyword1, keyword2 etc.

    Have I understood this correctly?
  • Bruno Desthuilliers

    #2
    Re: Passing arguments to subclasses

    John Dann a écrit :
    May I ask a simple newbie question, which I presume is true, but for
    which I can't readily find confirmation:
    >
    Let's say I have a parent class with an __init__ method explicitly
    defined:
    >
    class ParentClass(obj ect):
    def __init__(self, keyword1, keyword2):
    etc
    >
    and I subclass this:
    >
    class ChildClass(Pare ntClass):
    # No __init__ method explicitly defined
    >
    Now I presume that I can instantiate a child object as:
    >
    child = ChildClass(arg1 , arg2)
    >
    and arg1, arg2 will be passed through to the 'constructor' of the
    antecedent ParentClass (there being no overrriding __init__ method
    defined for ChildClass) and mapping to keyword1, keyword2 etc.
    >
    Have I understood this correctly?
    Yes. But you could have checked by yourself:

    bruno@bruno:~$ python
    Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
    [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>class Parent(object):
    .... def __init__(self, kw1, kw2):
    .... print self, kw1, kw2
    .... self.kw1, self.kw2 = kw1, kw2
    ....
    >>class Child(Parent): pass
    ....
    >>c = Child("arg1", "arg2")
    <__main__.Chi ld object at 0xb7ddecccarg1 arg2
    >>c.kw1
    'arg1'
    >>c.kw2
    'arg2'
    >>>
    HTH

    Comment

    • Gary Herron

      #3
      Re: Passing arguments to subclasses

      John Dann wrote:
      May I ask a simple newbie question, which I presume is true, but for
      which I can't readily find confirmation:
      >
      Let's say I have a parent class with an __init__ method explicitly
      defined:
      >
      class ParentClass(obj ect):
      def __init__(self, keyword1, keyword2):
      etc
      >
      and I subclass this:
      >
      class ChildClass(Pare ntClass):
      # No __init__ method explicitly defined
      >
      Now I presume that I can instantiate a child object as:
      >
      child = ChildClass(arg1 , arg2)
      >
      and arg1, arg2 will be passed through to the 'constructor' of the
      antecedent ParentClass (there being no overrriding __init__ method
      defined for ChildClass) and mapping to keyword1, keyword2 etc.
      >
      Have I understood this correctly?

      Yes, but...

      The nice things about Python is that you can use the interactive
      interpreter to test such things in an instant. (And not have to wait
      for a response from python-list. Like this:

      >>class ParentClass(obj ect):
      .... def __init__(self, keyword1, keyword2):
      .... print 'ParentClass.__ init__ called with', keyword1, keyword2
      ....
      >>class ChildClass(Pare ntClass):
      .... pass
      ....
      >>child = ChildClass(123, 456)
      ParentClass.__i nit__ called with 123 456
      >>>

      Gary Herron


      Comment

      • John Dann

        #4
        Re: Passing arguments to subclasses

        Thanks for the responses - they're much appreciated. And I understand
        the slight impatience with questions that could possibly be answered
        without recourse to a forum - I'm usually in the opposite position of
        fielding many newbie questions in a forum in a completely different
        field!

        But don't be too harsh on this category of questions for a couple of
        reasons. First, newbies don't necessarily yet have the same easy
        familiarity with the Python interpreter that's implied. - personally
        I'd be a little concerned as to whether any significant block of
        test/example code that I'd entered into the interpreter was actually
        addressing the exact question that I had in mind.

        And second, the answer might have been of the 'yes, but' kind. In
        other words, it might perhaps have been true in a certain sort of
        simple example, but one that failed to provide the complete picture.
        So sometimes it's reassuring to be able to get an authoritative
        answer.

        Thanks again for taking the time to answer.

        Comment

        • Scott David Daniels

          #5
          Re: Passing arguments to subclasses

          John Dann wrote:
          ... the answer might have been of the 'yes, but' kind.
          Well, if you really care, there is a 'yes, but' answer, but it
          only has to do with multiple inheritance, and hence is a bit
          esoteric for the issues you are currently addressing.

          This is not meant to be a tease; I think it would take pages to
          address the issue in a comprehensible way for someone who hasn't
          thought about the issues. If you are curious enough to pursue it,
          image some issues, design experiments, and see if you can ask a
          coherent question; if not, "YAGNI" (You Aint Gonna Need It).

          --Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          Working...