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

David Abrahams dave at boost-consulting.com
Sun Jul 6 02:18:39 CEST 2003


Stefan Seefeld <seefeld at sympatico.ca> writes:

> 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.

Could you be specific about the ending point of the text you'd like
to see incorporated as well as the beginning point?

> 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.

That's not really in the paper, is it?

> 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.

Sure, I agree.

>>>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.

That's a good idea.  Joel, what do you think about mentioning in the
tutorial that wrapped C++ objects are created by calling their
__init__ functions?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list