Skip to content

Commit d294a0e

Browse files
pydrake doc: Add notes on pybind11 Python types
1 parent 65eb489 commit d294a0e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

bindings/pydrake/pydrake_pybind.h

+29
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,42 @@ namespace pydrake {
1212
/**
1313
@page python_bindings Python Bindings
1414
15+
# Overview
16+
1517
Drake uses [pybind11](http://pybind11.readthedocs.io/en/stable/) for binding
1618
its C++ API to Python.
1719
1820
At present, a fork of `pybind11` is used which permits bindings matrices with
1921
`dtype=object`, passing `unique_ptr` objects, and prevents aliasing for Python
2022
classes derived from `pybind11` classes.
2123
24+
## `pybind11` Tips
25+
26+
### Python Types
27+
28+
Throughout the Drake code, Python types provided by `pybind11` are used, such
29+
as `py::handle`, `py::object`, `py::module`, `py::str`, `py::list`, etc.
30+
For an overview, see the
31+
[pybind11 reference](http://pybind11.readthedocs.io/en/stable/reference.html).
32+
33+
All of these are effectively thin wrappers around `PyObject*`, and thus can be
34+
cheaply copied.
35+
36+
Mutating the referred-to object also does not require passing by reference, so
37+
you can always pass the object by value for functions, but you should document
38+
your method if it mutates the object in a non-obvious fashion.
39+
40+
### Python Type Conversions
41+
42+
You can implicit convert between `py::object` and its derived classes (such
43+
as `py::list`, `py::class_`, etc.), assuming the actual Python types agree.
44+
You may also implicitly convert from `py::object` (and its derived classes) to
45+
`py::handle`.
46+
47+
If you wish to convert a `py::handle` (or `PyObject*`) to `py::object` or a
48+
derived class, you should use
49+
[`py::reinterpret_borrow<>`](http://pybind11.readthedocs.io/en/stable/reference.html#_CPPv218reinterpret_borrow6handle).
50+
2251
# Conventions
2352
2453
## Target Conventions

0 commit comments

Comments
 (0)