什么是SwigPyObject对象?

2024-09-24 02:16:57 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试用保存自定义对象时出错

with open(path + '/' + 'my_object.pickle', 'wb') as handle: pickle.dump(my_class_instance, handle, protocol=pickle.HIGHEST_PROTOCOL)

错误消息是:

TypeError: can't pickle SwigPyObject objects

我的第一个问题是:什么是SwigPyObject对象?,这样我就可以试着找出错误的来源。在


Tags: path对象instanceobjectmyas错误with
1条回答
网友
1楼 · 发布于 2024-09-24 02:16:57

斯威格是什么?

<^ >Simplified Wrapper and Interface Generator(SWIG)是一种开源软件工具,用于连接C或C++编写的库与脚本语言,如Python。一个著名的替代品是Boost。{a2}的目标是完成相同的任务。在

使用SWIG创建一个在C/C++中开发的Python库(用于与现有C/C++库的互操作性,或者在性能上获得增益,或者其他什么)。在

引用SWIG documentation

To build Python extension modules, SWIG uses a layered approach in which parts of the extension module are defined in C and other parts are defined in Python. The C layer contains low-level wrappers whereas Python code is used to define high-level features.

This layered approach recognizes the fact that certain aspects of extension building are better accomplished in each language (instead of trying to do everything in C or C++). Furthermore, by generating code in both languages, you get a lot more flexibility since you can enhance the extension module with support code in either language.

什么是SwigPyObject对象?

SwigPyObject对象在SWIG documentation中的“Python类接口的进一步细节”主题中描述。在

这个对象是一个实现细节。它由以下C结构定义:

typedef struct {
    PyObject_HEAD
    void *ptr;
    swig_type_info *ty;
    int own;
    PyObject *next;
    PyObject *dict;
} SwigPyObject;
< P> A^ { CD1>}对象存储C++对象的实例:

When -builtin is used, the pure python layer is stripped off. Each wrapped class is turned into a new python built-in type which inherits from SwigPyObject, and SwigPyObject instances are returned directly from the wrapped methods.

pickle文件

pickle文件中包含的数据包含一个SwigPyObject对象,但是您的解释器不知道如何反序列化它。您需要检查您的VielalEnv包含Python/C++库,它提供您的泡菜对象。在

相关问题 更多 >