• Dean Moldovan's avatar
    Add py::isinstance<T>(obj) for generalized Python type checking · b4498ef4
    Dean Moldovan authored
    Allows checking the Python types before creating an object instead of
    after. For example:
    ```c++
    auto l = list(ptr, true);
    if (l.check())
       // ...
    ```
    The above is replaced with:
    ```c++
    if (isinstance<list>(ptr)) {
        auto l = reinterpret_borrow(ptr);
        // ...
    }
    ```
    
    This deprecates `py::object::check()`. `py::isinstance()` covers the
    same use case, but it can also check for user-defined types:
    ```c++
    class Pet { ... };
    py::class_<Pet>(...);
    
    m.def("is_pet", [](py::object obj) {
        return py::isinstance<Pet>(obj); // works as expected
    });
    ```
    b4498ef4
test_inheritance.cpp 3.05 KB