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

Stefan Seefeld seefeld at sympatico.ca
Fri Jul 4 17:31:54 CEST 2003


David Abrahams wrote:

>>something that I'd like to understand is the relationship between the
>>C++ classes / objects and the corresponding python classes / objects.
> 
> 
> Is that covered in the paper?  If so, *specifically* which sections
> of text would you like to see "copied over?"

Well, partly. The section entitled 'Inheritance' discusses the
initialization of the base class through a call to it's __init__ method,
and you specifically talk about the lookup of the base class:

"Because C++ object construction is a one-step operation, C++
  instance data cannot be constructed until the arguments are available
in the __init__ function..."

This and the following paragraphs make some very important points.

As a C++ programmer I'm used to a specific object model / type system,
so I believe it would be very useful to discuss how that maps to python,
and how boost.python does the dispatching.

You may consider this a detail especially for first-time users, but
it really helps to understand these things to be efficient in the use
of boost.python.


>>I noted that I can call the parent's __init__ function multiple times,
>>which invokes the C++ base class constructor. It seems that way I can
>>instantiate more than one base class objects. Is that a specific
>>boost.python problem/feature or is that part of the python object model
>>? (It's hard to debug python, i.e. I can't log the 'this pointer' in
>>python, so I only know from boost.python that there are multiple C++
>>base instances)....
> 
> 
> The fact that you can call __init__ as many times as you want is part
> of the Python model.  The fact that, for a wrapped class, it creates a
> C++ object each time is part of the way Boost.Python works.  It might
> be possible to issue an exception when that happens but it hardly
> seems worthwhile to me.

Ok. But I do believe that it should be explained. Forgetting to call
__init__ on the base class may be a common error, especially for C++
programmers who are used to things like default constructors.


Thanks,
		Stefan





More information about the Cplusplus-sig mailing list