Skip to content

Commit

Permalink
Add quick blurb about __init__ with inherited types.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Aug 6, 2017
1 parent ce775c0 commit d8c1598
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/advanced/classes.rst
Expand Up @@ -144,6 +144,29 @@ a virtual method call.
>>> call_go(c)
u'meow! meow! meow! '
If you are defining a custom constructor in a derived Python class, you *must*
ensure that you explicitly call the bound C++ constructor using ``__init__``,
*regardless* of whether it is a default constructor or not. Otherwise, the
memory for the C++ portion of the instance will be left uninitialized, which
could do nasty things, such as give you a corrupted ``vtable``.

Here is an example:

.. code-block:: python
class Dachschund(Dog):
def __init__(self, name):
Dog.__init__(self) # Without this, a segfault may occur.
self.name = name
def bark(self):
return "yap!"
Note that a direct ``__init__`` constructor *should be called*, and ``super()``
should not be used. For simple cases of linear inheritance, ``super()``
may work, but once you begin mixing Python and C++ multiple inheritance,
things will fall apart due to differences between Python's MRO and C++'s
mechanisms.

Please take a look at the :ref:`macro_notes` before using this feature.

.. note::
Expand Down

0 comments on commit d8c1598

Please sign in to comment.