feat: py::type::of<T>() and py::type::of(h) (#2364)

* feat: type<T>()

* refactor: using py::type as class

* refactor: py::object as base

* wip: tigher api

* refactor: fix conversion and limit API further

* docs: some added notes from @EricCousineau-TRI

* refactor: use py::type::of
This commit is contained in:
Henry Schreiner
2020-09-14 18:06:26 -04:00
committed by GitHub
parent 32bb9071aa
commit f12ec00d70
6 changed files with 108 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
.. _type-conversions:
Type conversions
################

View File

@@ -1232,3 +1232,21 @@ appropriate derived-class pointer (e.g. using
more complete example, including a demonstration of how to provide
automatic downcasting for an entire class hierarchy without
writing one get() function for each class.
Accessing the type object
=========================
You can get the type object from a C++ class that has already been registered using:
.. code-block:: python
py::type T_py = py::type::of<T>();
You can directly use ``py::type::of(ob)`` to get the type object from any python
object, just like ``type(ob)`` in Python.
.. note::
Other types, like ``py::type::of<int>()``, do not work, see :ref:`type-conversions`.
.. versionadded:: 2.6