English 中文(简体)
Operator= in Boost::Python
原标题:

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 )

.srcPython.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
        ]
        dependscommonincludeoost/python/class.hpp(265) : see declaration of  boost::python::class_<W>::def 
        with
        [
            W=Foo
        ]
.srcPython.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
        ]
        dependscommonincludeoost/python/class.hpp(249) : see declaration of  boost::python::class_<W>::def 
        with
        [
            W=Foo
        ]
.srcPython.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
        ]
        dependscommonincludeoost/python/class.hpp(242) : see declaration of  boost::python::class_<W>::def 
        with
        [
            W=Foo
        ]
.srcPython.cpp(12) : error C2780:  boost::python::class_<W> &boost::python::class_<W>::def(const char *,F)  : expects 2 arguments - 1 provided
        with
        [
            W=Foo
        ]
        dependscommonincludeoost/python/class.hpp(233) : see declaration of  boost::python::class_<W>::def 
        with
        [
            W=Foo
        ]
.srcPython.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
        ]
        dependscommonincludeoost/python/class.hpp(223) : see declaration of  boost::python::class_<W>::def 
        with
        [
            W=Foo
        ]
最佳回答

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)

问题回答

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)

The previous answer using Foo::operator= directly does not work. Do this instead:

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

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

Usage in python:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签