2009-12-18 3 views
1

나는 다음 클래스연산자 = 부스트의 :: 파이썬

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

부스트 :: 파이썬을 사용하여 파이썬에 해당 기능을 수출 할 수있는 간단한 방법이 있을까요 같은이 있다면? 문서는 나열하지 않고 좋은

.def(self = self) 

쉽게 나는이 정직하게 필요한 경우 내가 알지도 못하는 파이썬 전문가 아닙니다. 하지만 난 내 파이썬 스크립트 에서이 기능을 원한다. 그래서 나는 단지 질문을 게시하고있다.

편집 : 나는 .DEF을 수행 할 때 여기

컴파일러 오류가 있습니다 (자기 = 자기)

.\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

내가 파이썬 전문가가 아니지만, 파이썬에서 연산자와 허식 " = "은 C++에서와 같은 의미가 아닙니다 : a=b은 동일한 내부 객체에 대한 새로운 참조를 생성하므로, 파이썬 인터페이스에 C++의 을 내보낼 필요가 없습니다.
개체 복제본을 반환하는 "clone"멤버 함수 (operator=으로 구현 됨)를 만드는 것입니다. 그리고이 함수를 파이썬으로 임포트하십시오. foo2 = Foo(foo1) (물론이 생성자는 C++/파이썬 인터페이스에 정의되어 있어야합니다) 당신은 파이썬에 할당 연산자를 노출 할 필요가 없습니다

+0

좋아 좋은, 이것이 내가 알 필요가 정확히 무엇이다. 저는 파이썬 전문가가 아니므로 어떻게 a = b가 작동했는지 알지 못했습니다. 저는 실제로 복사 생성자와 복사 메서드가 있습니다. 연산자 = 및 복사 생성자 둘 다 copy()를 호출하므로 복사 생성자를 내보내고 호출해야합니다. 감사합니다. – Charles

4

:
는 다른 방법으로, 파이썬에서, 당신은 복사 생성자를 사용할 수 있습니다. 파이썬에서 할당 연산자는 기존 객체를 다른 객체의 사본으로 다시 할당하지 않고 이름을 다른 객체에 다시 할당합니다. 파이썬에서는 새 오브젝트를 작성하거나 copy of another one 인 새 오브젝트를 작성합니다. 이는 복사 생성자와 유사합니다. 어떤 이유로 당신이 파이썬에서 C++ 할당 연산자를 호출해야하는 경우

, 당신은 아마 이런 것을 할 수있는 멤버 함수를 추가합니다 (자신이 시도하지 않은) 할 수

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

그런 것 파이썬에서 수동으로 호출 :

f1 = Foo() 
f2 = Foo() 
f2.reassign(f1) 
+0

좋은 정보를 다시 한 번 말하면서 나는 명확하게 Foo :: operator =를 시도하지 않았다. 왜냐하면 내가 해킹에 대해 다소 위험하고 라운드 적이기 때문이다 : p 파이썬의 = 작동 방식을 알았으므로 이제는 더 이상 걱정할 필요가 없다. 감사! – Charles

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)