[C++-sig] Fwd: Passing Python-derived class back into a native C++ class doesn't match the C++ signature

Nat Goodspeed nat at lindenlab.com
Sat Nov 12 21:41:04 CET 2011


On Nov 12, 2011, at 2:25 PM, Jim Bosch <talljimbo at gmail.com> wrote:

> On 11/12/2011 02:12 PM, Adam Preble wrote:
>> Woooah that looks like it does work, although I had to interpret it like so:
>> 
>> class DerivedTest(BaseTest):
>>   def __init__(self):
>>     BaseTest.__init__(self)

Yes, sorry the email quoting confused my intended indentation.

>> Is this kind of the normal rigors when subclassing in Python
>> normally, or is this something I must particularly pay attention to when
>> using Boost?
> 
> Calling the base class __init__ is less important in pure Python (i.e. sometimes it's not necessary), but it's still definitely good practice; it depends on whether the base class __init__ does anything important. Python does not ever call it automatically, and in Boost.Python it definitely does something important.

That's right: this is NOT specific to Boost.Python. If your Python subclass constructor doesn't explicitly call the base-class constructor, the base class remains uninitialized. As Jim says, with Boost.Python the bad effect of an uninitialized base can include that type recognition failure.

fwiw, if the only logic in your subclass constructor is to forward all the same arguments to the base-class constructor, you could omit it entirely and just let Python implicitly call the inherited base-class constructor. I only define a subclass constructor when it must perform additional logic beyond initializing the base class. Again, that's a general rule in Python rather than anything specific to Boost.Python.
> 


More information about the Cplusplus-sig mailing list