You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The augmented assignment operators (operator+=, etc) on py::object are implemented analogously to the regular binary operators (operator+, etc): they call one of the PyNumber_ API functions and return the result. For immutable types such as py::str, PyNumber_InPlaceAdd is implemented the same as PyNumber_Add, so you need to look at the result in order to see any change; in both C++ and Python, though, foo += bar is supposed to be sufficient to update foo on its own without observing the result of the expression.
Reproducible example code
#include <pybind11/pybind11.h>
namespace py = pybind11;
using namespace pybind11::literals;
PYBIND11_MODULE(example, m) {
m.def("test", []() -> py::str {
py::str text = "foo"_s;
text += " bar"_s;
return text;
});
}
// import example
// assert example.test() == "foo bar"
// --> fails: example.test() actually returns "foo"
The text was updated successfully, but these errors were encountered:
Required prerequisites
Problem description
The augmented assignment operators (
operator+=
, etc) onpy::object
are implemented analogously to the regular binary operators (operator+
, etc): they call one of thePyNumber_
API functions and return the result. For immutable types such aspy::str
,PyNumber_InPlaceAdd
is implemented the same asPyNumber_Add
, so you need to look at the result in order to see any change; in both C++ and Python, though,foo += bar
is supposed to be sufficient to updatefoo
on its own without observing the result of the expression.Reproducible example code
The text was updated successfully, but these errors were encountered: