[C++-sig] Inheritance and __init__

English, Mark Mark.English at rbccm.com
Wed Oct 31 12:07:33 CET 2007


> -----Original Message-----
> From: c++-sig-bounces at python.org 
> [mailto:c++-sig-bounces at python.org] On Behalf Of Bue Vedel-Larsen
> Sent: 31 October 2007 10:46
> To: c++-sig at python.org
> Subject: Re: [C++-sig] Inheritance and __init__
> 
> >> 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
As far as I know, the Python code working is undefined behaviour.
Whenever overriding __init__() (especially for new style classes which
Boost Python class_ instances implicitly are), call the base class
__init__() passing through arguments as necessary.

If you don't do this, the __init__ of the base class won't get called
which for Boost Python is bad news.

Try this:
class Base(object):
    def __init__(self):
        super(Base, self).__init__()
        self.data = 1
class Derived(Base):
    def __init__(self):
        print 'I am derived'
>>> d = Derived() #Happy
I am derived
>>> d.data #Unhappy

Cheers,
mE
________________________________________

This E-Mail (including any attachments) may contain privileged or confidential information.  It is intended only for the addressee(s) indicated above.

The sender does not waive any of its rights, privileges or other protections respecting this information.  

Any distribution, copying or other use of this E-Mail or the information it contains, by other than an intended recipient, is not sanctioned and is prohibited.

If you received this E-Mail in error, please delete it and advise the sender (by return E-Mail or otherwise) immediately. 

This E-Mail (including any attachments) has been scanned for viruses. 

It is believed to be free of any virus or other defect that might affect any computer system into which it is received and opened. 

However, it is the responsibility of the recipient to ensure that it is virus free. 

The sender accepts no responsibility for any loss or damage arising in any way from its use.

E-Mail received by or sent from RBC Capital Markets is subject to review by Supervisory personnel. 

Such communications are retained and may be produced to regulatory authorities or others with legal rights to the information.

IRS CIRCULAR 230 NOTICE:  TO COMPLY WITH U.S. TREASURY REGULATIONS, WE ADVISE YOU THAT ANY U.S. FEDERAL TAX ADVISE INCLUDED IN THIS COMMUNICATION IS NOT INTENDED OR WRITTEN TO BE USED, AND CANNOT BE USED, TO AVOID ANY U.S. FEDERAL TAX PENALTIES OR TO PROMOTE, MARKET, OR RECOMMEND TO ANOTHER PARTY ANY TRANSACTION OR MATTER.



More information about the Cplusplus-sig mailing list