Replies: 1 comment 2 replies
-
Hi @jgannon65 , you can always "reverse-engineer" original code of pybind11. So going to the code snippet: #include <pybind11/pybind11.h>
namespace py = pybind11;
enum Animal
{
Dog = 0,
Cat = 1
};
enum House
{
Door = 0,
Floor = 1
};
PYBIND11_MODULE(foo, module)
{
py::enum_<House> house(module, "House");
house.value("Door", House::Door);
house.value("Floor", House::Floor);
house.export_values();
py::enum_<Animal> animal(module, "Animal");
animal.value("Dog", Animal::Dog);
animal.value("Cat", Animal::Cat);
animal.export_values();
animal.def(
"__str__", [](const py::object &arg) -> py::str
{
py::handle type = py::type::handle_of(arg);
py::object type_name = type.attr("__name__");
return py::str("{} called {} with number {}").format(type_name, py::detail::enum_name(arg), py::int_(arg));
},
py::name("__str__"), py::is_method(module));
animal.def(
"__repr__", [](const py::object &arg) -> py::str
{ return py::str("__repr__ of {}").format(py::int_(arg)); },
py::name("__repr__"), py::is_method(module));
}
I added a animal.attr("__repr__") = py::cpp_function([](const py::object &arg) -> py::str
{ return py::str("__repr__ of {}").format(py::int_(arg)); },
py::name("__repr__"), py::is_method(module)); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a lot of enum's that are being used as bitmasks in my program. I've created a _enum for them, and it works fine, except when there are multiple values set, the str and repr method outputs ???. I would like to override these methods, but it doesn't seem to be working. Is there a way to work around this?
Beta Was this translation helpful? Give feedback.
All reactions