[C++-sig] deriving in python from a C++ base class

Stefan Seefeld seefeld at sympatico.ca
Wed Jul 2 05:35:09 CEST 2003


Ralf W. Grosse-Kunstleve wrote:
> --- Stefan Seefeld <seefeld at sympatico.ca> wrote:
> 
>>class MyWorld(World):
>>     def __init__(self):
>>         self.set('hi there')
>>         print self.greet()
> 
> 
> I think you have to call World.__init__() before you can use the instance.

playing more with the code, I'm observing the following:

* instantiating 'MyWorld' in python will not call the World's constructor

* instantiating a 'MyWorld' object from within C++ will call the constructor

* adding a call to 'World.__init__(self)' into the definition of MyWorld.__init__
   will call World's constructor twice, or, more generally, n + 1 times, if
   n is the number of times I call World.__init__.

That seems to me quite error-prone and I'm wondering why it is done the way it
is. More than error-prone, I'm wondering how I could make it such that I can
instantiate 'MyWorld' from python *and* C++, and still get the base constructor
called once per instance in both cases.

Thanks,
		Stefan

PS: by the way, I found the article you cite very useful, and I think that
     it illustrates things that are not or less clearly documented in the
     tutorial / reference. May be some of the text could be copied over...





More information about the Cplusplus-sig mailing list