| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -422,15 +422,24 @@ The section on :ref:`properties` discussed the creation of instance properties | |||
| 422 | 422 | that are implemented in terms of C++ getters and setters. | |
| 423 | 423 | ||
| 424 | 424 | 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: | ||
| 430 | 439 | ||
| 431 | 440 | .. code-block:: cpp | |
| 432 | 441 | ||
| 433 | - py::class_<Foo>(m, "Foo") | ||
| 442 | + py::class_<Foo>(m, "Foo", py::metaclass()) | ||
| 434 | 443 | .def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); }); | |
| 435 | 444 | ||
| 436 | 445 | Operator overloading | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,7 +33,7 @@ completely avoid copy operations with Python expressions like | |||
| 33 | 33 | ||
| 34 | 34 | .. code-block:: cpp | |
| 35 | 35 | ||
| 36 | - py::class_<Matrix>(m, "Matrix") | ||
| 36 | + py::class_<Matrix>(m, "Matrix", py::buffer_protocol()) | ||
| 37 | 37 | .def_buffer([](Matrix &m) -> py::buffer_info { | |
| 38 | 38 | return py::buffer_info( | |
| 39 | 39 | m.data(), /* Pointer to buffer */ | |
@@ -46,9 +46,12 @@ completely avoid copy operations with Python expressions like | |||
| 46 | 46 | ); | |
| 47 | 47 | }); | |
| 48 | 48 | ||
| 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. | ||
| 52 | 55 | ||
| 53 | 56 | .. code-block:: cpp | |
| 54 | 57 | ||
@@ -77,7 +80,7 @@ buffer objects (e.g. a NumPy matrix). | |||
| 77 | 80 | typedef Matrix::Scalar Scalar; | |
| 78 | 81 | constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit; | |
| 79 | 82 | ||
| 80 | - py::class_<Matrix>(m, "Matrix") | ||
| 83 | + py::class_<Matrix>(m, "Matrix", py::buffer_protocol()) | ||
| 81 | 84 | .def("__init__", [](Matrix &m, py::buffer b) { | |
| 82 | 85 | typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides; | |
| 83 | 86 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments