views:

251

answers:

2

If I have something like the following class

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

Is there an easy way to export that functionality to python using boost::python? The documentation does not list and nice and easy

.def( self = self )

I am not an expert with python so I do not even know if this is necessary to be honest. But I want this functionality in my python scripts, so I am posting the question just to make sure.

Edit:

here are the compiler errors when I do do .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
        ]
+1  A: 

I'm not an expert with Python, but in python the affectation with operator "=" has not the same meaning as in C++: a=b creates a new reference to the same internal object, so there is no point in exporting c++'s operator= into python interface.
What you may do is creating a "clone" member function (implemented in terms of operator=) that will return a duplicate of the object. And import this function into Python.
Alternatively, in Python, you can use a copy constructor: foo2 = Foo(foo1) (of course this constructor must be defined in the c++/python interface)

rafak
Ok good, this is exactly what I needed to know. I am not an expert with python so I did not know how a=b worked.I actually do have a copy constructor and a copy method. operator= and the copy constructor both call copy(), so I guess I just need to export the copy constructor and call it good, thanks!
Charles
+2  A: 

You should not need to expose your assignment operator to python. In python, the assignment operator doesn't re-assign an existing object as a copy of another one, it re-assigns the name to a different object. In python, you are either creating a new object, or create a new object that is a copy of another one, which is more akin to a copy constructor.

If for some reason you need to invoke the C++ assignment operator from python, you can probably (haven't tried it myself) add a member function to do that like this:

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

You would then invoke it manually in python:

f1 = Foo()
f2 = Foo()
f2.reassign(f1)
zdan
Good info again, I have not tried explicitly Foo::operator= because I thought that to be a rather dangerous and round about hack :pNow that I know how python's = works I know I no longer need to worry about it, thanks!
Charles