Skip to content

For Python subclass, forgetting to call C++ superclass __init__ constructor is painful #1577

@EricCousineau-TRI

Description

@EricCousineau-TRI

While it is noted here in the docs:
https://pybind11.readthedocs.io/en/stable/advanced/classes.html#overriding-virtual-functions-in-python
it is still sometimes easy to forget. If it were somehow possible, it'd be nice to robustly detect access to unconstructed base classes. (... though it may be a big performance penalty, so I'm fine with this being closed as "Not a Problem")

Example failure case from above:

class Dachschund(Dog):
    def __init__(self, name):
        # Oops!
        self.name = name

    def bark(self):
        return "yap!"

d = Dog("Sir Schnitzel")
d.go(10)  # Nuts!

Had run into it when writing this example, and then felt dumb once I realized what the problem was.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions