@@ -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
0 commit comments