如何将默认的 numpy 数组参数传递给 pybind11 中的函数?

How to pass a default numpy array argument to a function in pybind11?

我正在定义一个以 py::array_t 和 py::array_t 作为参数的方法。我如何告诉 pybind11 将这些参数默认为我选择的数组? (比如说 np.array([0, 0, 0]))

我尝试通过 "Argument"_a = py::array_T({0, 0, 0}) 添加默认值 但是当我调用它时它告诉我 'array has incorrect number of dimensions: 3; expected 1'

m.def("foo", [](py::array_t<double> Arg1,                        
                py::array_t<bool> Arg2){

        auto bar = Arg1.unchecked<1>();
        auto bar2 = Arg2.unchecked<1>();

                    '''other simple code that doesn't alter bar or bar2'''

        return bar;
    },
    "Arg1"_a,
    "Arg2"_a = py_array<bool> ({0, 0, 0})
);

问题是您的默认参数值是长度为零的 3 维数组,而不是三个元素的 1 维数组。

您正在调用的构造函数 py_array<bool> ({0, 0, 0}):

    explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
        : array_t(private_ctor{}, std::move(shape),
                ExtraFlags & f_style ? f_strides(*shape, itemsize()) : c_strides(*shape, itemsize()),
                ptr, base) { }

https://github.com/pybind/pybind11/blob/c9d32a81f40ad540015814edf13b29980c63e39c/include/pybind11/numpy.h#L861