[C++-sig] Inheritance and __init__

Bue Vedel-Larsen bue.longbow at gmail.com
Wed Oct 31 11:45:56 CET 2007


"English, Mark" <Mark.English at rbccm.com> wrote in
news:EE7D40281B517A41AF5A90847A61F27937965615 at SEW39051.oak.fg.rbc.com: 
>> 
>> I'm having a strange problem with Boost::Python. Consider the 
>> following 
>> Python code:
>> 
>> class Base:
>>      def foo(self, str):
>>           print str
>>           
>> class Derived(Base):
>>      def __init__(self):
>>           self.foo('__init__')
>> 
>> d = Derived()
>> 
<snip>
>>     // This doesn't work
>>     run_script( 
>>         "class DerivedB(Base):\n"
>>         "\tdef __init__(self):\n"
>>         "\t\tself.foo('In Python: DerivedB.__init__()')\n"
>>         "dB = DerivedB()\n"
>>         );
> Does it make a difference if in the __init__ methods you call
> "super(KlassName, self).__init__()" first ?
> E.g.
> "class DerivedB(Base):\n"
> "\tdef __init__(self):\n"
> "\t\tsuper(DerivedB, self).__init__()
> "\t\tself.foo('In Python: DerivedB.__init__()')\n"

Amazing, adding "super(DerivedB, self).__init__()" or "Base.__init__
(self)" before self.foo('In Python: DerivedB.__init__()') makes it work.

But why is this necessary? The pure Python example works without 
explicitly calling the __init__ method of the Base class. Do my class_ 
definition of the module lack something?

Regards,
Bue




More information about the Cplusplus-sig mailing list