Skip to content

Commit 00087fd

Browse files
committed
Adding mention of conversion to docs
1 parent 8e279ac commit 00087fd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

docs/classes.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ The entries defined by the enumeration type are exposed in the ``__members__`` p
485485
>>> Pet.Kind.__members__
486486
{'Dog': Kind.Dog, 'Cat': Kind.Cat}
487487
488+
489+
You can also access the enumeration using a string using the enum's constructor,
490+
such as ``Pet('Cat')``. This makes it possible to automatically convert a string
491+
to an enumeration in an API if the enumeration is marked implicitly convertible
492+
from a string, with a line such as:
493+
494+
.. code-block:: cpp
495+
496+
py::implicitly_convertible<std::string, Pet::Kind>();
497+
498+
Now, in Python, the following code will also correctly construct a cat:
499+
500+
.. code-block:: pycon
501+
502+
>>> p = Pet('Lucy', 'Cat')
503+
488504
.. note::
489505

490506
When the special tag ``py::arithmetic()`` is specified to the ``enum_``

0 commit comments

Comments
 (0)