Conversation
… default argument is not optional
|
I'm sort of ambivalent about including this (the C++ isn't onerous: template <typename T>
object get_with_default(T &&defaultv) const {
return obj.contains(key) ? *this : object_or_cast(std::forward<T>(defaultv));
}(Then you can use it for non- |
|
boost::python has the same function. I want to minimize the changes needed to transition from boost::python to pybind11. Also, it seems more intuitive to just mirror the existing Python function name and usage. Could you please elaborate more on the code you wrote? What would be the example usage of it? Is it like py::accessor(obj, key).get_with_default(defaultv)? |
|
py::object o = /* ... */;
py::object val = o["abc"].get_with_default("z"); |
|
Thanks. I see. The point of this MR is to mirror the existing Python and boost::python API, which I think is more intuitive than inventing a new API. |
|
Alternative to #2779 |
It works just like Python except that the default argument is not optional