Skip to content

Fix new-style __init__ usage in numpy docs #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ buffer objects (e.g. a NumPy matrix).
constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;

py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
.def("__init__", [](py::buffer b) {
.def(py::init([](py::buffer b) {
typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;

/* Request a buffer descriptor from Python */
Expand All @@ -101,8 +101,8 @@ buffer objects (e.g. a NumPy matrix).
auto map = Eigen::Map<Matrix, 0, Strides>(
static_cast<Scalar *>(info.ptr), info.shape[0], info.shape[1], strides);

return Matrix(m);
});
return Matrix(map);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bstaletic, so no new here, for you?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either works. Let me find the wording from the docs.

https://pybind11.readthedocs.io/en/stable/advanced/classes.html#custom-constructors

One constructor return Example by value, one returns it in a unique_ptr and one returns a raw owning pointer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fair enough. Doesn't matter too much, then, but the less pointers the better? I guess Eigen will make sure things are movable...

}));

For reference, the ``def_buffer()`` call for this Eigen data type should look
as follows:
Expand Down