Skip to content

Commit 407de1e

Browse files
committed
Shorter syntax from @YannickJadoul
1 parent 00087fd commit 407de1e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

include/pybind11/pybind11.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,13 +1378,12 @@ template <typename Type> class enum_ : public class_<Type> {
13781378
}, return_value_policy::copy);
13791379
def(init([](Scalar i) { return static_cast<Type>(i); }));
13801380
def(init([name, m_entries_ptr](std::string value) -> Type {
1381-
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
1382-
std::string key = pybind11::cast<pybind11::str>(kv.first);
1383-
if (value == key) {
1384-
return pybind11::cast<Type>(kv.second);
1385-
}
1386-
}
1387-
throw value_error("\"" + value + "\" is not a valid value for enum type " + name);
1381+
pybind11::dict values = reinterpret_borrow<pybind11::dict>(m_entries_ptr);
1382+
pybind11::str key = pybind11::str(value);
1383+
if (values.contains(key))
1384+
return pybind11::cast<Type>(values[key]);
1385+
else
1386+
throw value_error("\"" + value + "\" is not a valid value for enum type " + name);
13881387
}));
13891388
def("__int__", [](Type value) { return (Scalar) value; });
13901389
#if PY_MAJOR_VERSION < 3

0 commit comments

Comments
 (0)