Passing arguments to subclasses

J

John Dann

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(object):
def __init__(self, keyword1, keyword2):
etc

and I subclass this:

class ChildClass(ParentClass):
# 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?
 
B

Bruno Desthuilliers

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(object):
def __init__(self, keyword1, keyword2):
etc

and I subclass this:

class ChildClass(ParentClass):
# 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..... def __init__(self, kw1, kw2):
.... print self, kw1, kw2
.... self.kw1, self.kw2 = kw1, kw2
....
HTH
 
G

Gary Herron

John said:
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(object):
def __init__(self, keyword1, keyword2):
etc

and I subclass this:

class ChildClass(ParentClass):
# 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:

.... def __init__(self, keyword1, keyword2):
.... print 'ParentClass.__init__ called with', keyword1, keyword2
........ pass
....

Gary Herron
 
J

John Dann

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top