Passing arguments to subclasses
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon Jun 23 11:53:40 EDT 2008
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 at 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__.Child object at 0xb7ddeccc> arg1 arg2
>>> c.kw1
'arg1'
>>> c.kw2
'arg2'
>>>
HTH
More information about the Python-list
mailing list