Skip to content

Commit 6a113ab

Browse files
committed
update docs to describe new class_ tags
1 parent 4907f55 commit 6a113ab

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

docs/advanced/classes.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,24 @@ The section on :ref:`properties` discussed the creation of instance properties
422422
that are implemented in terms of C++ getters and setters.
423423

424424
Static properties can also be created in a similar way to expose getters and
425-
setters of static class attributes. It is important to note that the implicit
426-
``self`` argument also exists in this case and is used to pass the Python
427-
``type`` subclass instance. This parameter will often not be needed by the C++
428-
side, and the following example illustrates how to instantiate a lambda getter
429-
function that ignores it:
425+
setters of static class attributes. Two things are important to note:
426+
427+
1. Static properties are implemented by instrumenting the *metaclass* of the
428+
class in question -- however, this requires the class to have a modifiable
429+
metaclass in the first place. pybind11 provides a ``py::metaclass()``
430+
annotation that must be specified in the ``class_`` constructor, or any
431+
later method calls to ``def_{property_,∅}_{readwrite,readonly}_static`` will
432+
fail (see the example below).
433+
434+
2. For static properties defined in terms of setter and getter functions, note
435+
that the implicit ``self`` argument also exists in this case and is used to
436+
pass the Python ``type`` subclass instance. This parameter will often not be
437+
needed by the C++ side, and the following example illustrates how to
438+
instantiate a lambda getter function that ignores it:
430439

431440
.. code-block:: cpp
432441
433-
py::class_<Foo>(m, "Foo")
442+
py::class_<Foo>(m, "Foo", py::metaclass())
434443
.def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); });
435444
436445
Operator overloading

docs/advanced/pycpp/numpy.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ completely avoid copy operations with Python expressions like
3333

3434
.. code-block:: cpp
3535
36-
py::class_<Matrix>(m, "Matrix")
36+
py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
3737
.def_buffer([](Matrix &m) -> py::buffer_info {
3838
return py::buffer_info(
3939
m.data(), /* Pointer to buffer */
@@ -46,9 +46,12 @@ completely avoid copy operations with Python expressions like
4646
);
4747
});
4848
49-
The snippet above binds a lambda function, which can create ``py::buffer_info``
50-
description records on demand describing a given matrix. The contents of
51-
``py::buffer_info`` mirror the Python buffer protocol specification.
49+
Supporting the buffer protocol in a new type involves specifying the special
50+
``py::buffer_protocol()`` tag in the ``py::class_`` constructor and calling the
51+
``def_buffer()`` method with a lambda function that creates a
52+
``py::buffer_info`` description record on demand describing a given matrix
53+
instance. The contents of ``py::buffer_info`` mirror the Python buffer protocol
54+
specification.
5255

5356
.. code-block:: cpp
5457
@@ -77,7 +80,7 @@ buffer objects (e.g. a NumPy matrix).
7780
typedef Matrix::Scalar Scalar;
7881
constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;
7982
80-
py::class_<Matrix>(m, "Matrix")
83+
py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
8184
.def("__init__", [](Matrix &m, py::buffer b) {
8285
typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;
8386

0 commit comments

Comments
 (0)