Boost::Python中的Operator=

1 投票
3 回答
2375 浏览
提问于 2025-04-15 17:11

如果我有一个像下面这样的类

class Foo
{
private:
    int _bar;
public:
    Foo& operator=( const Foo& other )
    {
        _bar = other._bar;
        return *this;
    }
}

有没有简单的方法可以用boost::python把这个功能导出到python中?文档里没有列出简单易懂的方法。

.def( self = self )

老实说,我对python不是很精通,所以我甚至不知道这是否真的必要。但我想在我的python脚本中使用这个功能,所以我发这个问题只是为了确认一下。

编辑:

当我使用 .def( self = self ) 时,这里是编译错误信息

.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &,const A3 &)' : expects 5 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(265) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &)' : expects 4 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(249) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,A1,const A2 &)' : expects 3 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(242) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,F)' : expects 2 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(233) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2784: 'boost::python::class_<W> &boost::python::class_<W>::def(const boost::python::def_visitor<Derived> &)' : could not deduce template argument for 'const boost::python::def_visitor<Derived> &' from 'boost::python::self_ns::self_t'
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(223) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]

3 个回答

2

之前那个直接使用 Foo::operator= 的回答是不行的。你可以试试这样做:

void assignFoo(Foo& self, const Foo& other)
{
    self = other;
}

class_<Foo>("Foo")
    .def("assign", assignFoo);

python 中的用法:

foo, other = Foo(), Foo()
foo.assign(other)
3

你其实不需要在Python中暴露你的赋值操作符。在Python里,赋值操作符并不是把一个已有的对象复制成另一个对象,而是把一个名字重新指向一个不同的对象。在Python中,你要么是在创建一个新对象,要么是在创建一个新对象,这个新对象是另一个对象的复制,这更像是一个复制构造函数。

如果因为某种原因你需要从Python调用C++的赋值操作符,你可以尝试(我自己没试过)添加一个成员函数来实现,像这样:

.def("reassign", &Foo::operator=);

然后你可以在Python中手动调用它:

f1 = Foo()
f2 = Foo()
f2.reassign(f1)
3

我对Python不是很精通,但在Python中,使用“=”这个符号的意思和C++不一样:a=b 只是创建了一个新的引用,指向同一个内部对象,所以把C++的operator=导入到Python中其实没什么意义。
你可以做的是创建一个“克隆”成员函数(这个函数是基于operator=实现的),它会返回对象的一个副本。然后把这个函数导入到Python中。
另外,在Python中,你还可以使用复制构造函数:foo2 = Foo(foo1)(当然,这个构造函数必须在C++和Python的接口中定义)

撰写回答