gpt4 book ai didi

python - boost python enable_pickling 期望

转载 作者:行者123 更新时间:2023-11-30 05:18:27 27 4
gpt4 key购买 nike

您好,我正在使用 python 启动一个 cpp 类,该类使用 boost python lib 转换为可用的 python。同时,我需要 pickle 使用启用 python 的 cpp 类的 python 类。

所以我所做的是将 enable_picking() 添加到这样的示例类定义中:

class_<pform::base::Price>("Price", init<double>())
.def(self == self)
.def(self_ns::str(self_ns::self)) // __str__
.def("get_value", &pform::base::Price::get_value)

它使类变得可 pickle 。但是我在解开它时遇到了这个错误。

Boost.Python.ArgumentError: Python argument types in
Price.__init__(Price)
did not match C++ signature:
__init__(_object*, double)

那么这里缺少什么?

最佳答案

有点晚了,但我找到了相关的 boost 文档:

http://www.boost.org/doc/libs/1_64_0/libs/python/doc/html/reference/topics/pickle_support.html

The Pickle Interface

At the user level, the Boost.Python pickle interface involves three special methods:

  • __getinitargs__ When an instance of a Boost.Python extension class is pickled, the pickler tests if the instance has a __getinitargs__ method. This method must return a Python tuple (it is most convenient to use a boost::python::tuple). When the instance is restored by the unpickler, the contents of this tuple are used as the arguments for the class constructor. If __getinitargs__ is not defined, pickle.load will call the constructor (__init__) without arguments; i.e., the object must be default-constructible.

  • __getstate__ When an instance of a Boost.Python extension class is pickled, the pickler tests if the instance has a __getstate__ method. This method should return a Python object representing the state of the instance.

  • __setstate__ When an instance of a Boost.Python extension class is restored by the unpickler (pickle.load), it is first constructed using the result of __getinitargs__ as arguments (see above). Subsequently the unpickler tests if the new instance has a __setstate__ method. If so, this method is called with the result of __getstate__ (a Python object) as the argument.

The three special methods described above may be .def()'ed individually by the user. However, Boost.Python provides an easy to use high-level interface via the boost::python::pickle_suite class that also enforces consistency: __getstate__ and __setstate__ must be defined as pairs. Use of this interface is demonstrated by the following examples.

在您的特定示例中,该类不是默认可构造的,因为它需要一个 double 参数(我假设它是“值”)。要为 Python 包装它,您还需要定义:

.def("__getinitargs__", +[](pform::base::Price const& self){
return boost::python::make_tuple(self.get_value());
})

现在 Boost Python 将使用“值”初始化您的类;而不是调用默认构造函数 (pform::base::Price())。

关于python - boost python enable_pickling 期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com