1

次のクラスのようなものがあれば

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
        ]
4

3 に答える 3

4

代入演算子を python に公開する必要はありません。Python では、代入演算子は既存のオブジェクトを別のオブジェクトのコピーとして再割り当てするのではなく、名前を別のオブジェクトに再割り当てします。Python では、新しいオブジェクトを作成するか、別のオブジェクトのコピーである新しいオブジェクトを作成します。これは、コピー コンストラクターに似ています。

何らかの理由で Python から C++ 代入演算子を呼び出す必要がある場合は、おそらく (自分で試したことはありませんが) メンバー関数を追加して、次のようにすることができます。

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

次に、Python で手動で呼び出します。

f1 = Foo()
f2 = Foo()
f2.reassign(f1)
于 2009-12-18T21:52:04.440 に答える
3

私は Python の専門家ではありませんが、Python では演算子 "=" の影響は C++ と同じ意味ではありません:a=b同じ内部オブジェクトへの新しい参照を作成するため、C++operator=を Python インターフェイスにエクスポートしても意味がありません。
あなたができることはoperator=、オブジェクトの複製を返す「クローン」メンバー関数 ( に関して実装) を作成することです。そして、この関数を Python にインポートします。
あるいは、Python では、コピー コンストラクターを使用できますfoo2 = Foo(foo1)(もちろん、このコンストラクターは c++/python インターフェイスで定義する必要があります)。

于 2009-12-18T21:48:30.487 に答える
1

直接使用した前の回答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)
于 2015-01-24T18:07:37.207 に答える