python - Boost Python 重写相等运算符

标签 python c++ boost boost-python

我正在尝试覆盖通过 boost python 公开的类的 python 相等运算符。

所以我的代码看起来像这样:

   class_<MyClass, boost::noncopyable, boost::shared_ptr<MyClass> >("MyClass", no_init)
    .def("foo", &MyClass::foo)                                    
     .
     .
     .
    .def("__eq__", &MyClass::operator==) 
    .def("__ne__", &MyClass::operator!=)

然而,在 python 中,当我获取代表相同 C++ 对象但来自不同 python 对象的对象的 2 个实例时,它们永远不相等...

因此:

from myPackage import myClass

v1 = myClass.get("abc")
v2 = myClass.get("abc")
if v1 == v2:
   print "true"
else:
   print "false"

始终打印 false。 (为了简单起见,我省略了对象中的 get 函数定义)

有什么想法吗?

最佳答案

考虑为 MyClass::operator==() 编写一个 C++ 测试用例来验证其实现。公开运算符的 Boost.Python 代码是正确的。


这是一个示例 demonstrating将 C++ 类的比较运算符公开为等式和不等式丰富比较方法:

#include <iostream>
#include <string>
#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>

class foo
{
public:
  foo(const char* value) : value_(value) {}
  foo(const foo&) = delete;
  foo& operator=(const foo&) = delete;

  bool operator==(const foo& rhs) 
  {
    std::cout << "foo::operator==()" << std::endl;
    return value_ == rhs.value_;
  }

  bool operator!=(const foo& rhs)
  {
    std::cout << "foo::operator!=()" << std::endl;  
    return !(*this == rhs);
  }

  std::string get_value() const     { return value_;  }
  void set_value(std::string value) { value_ = value; }

private:
  std::string value_;
};

boost::shared_ptr<foo> make_foo(const char* value)
{
  return boost::make_shared<foo>(value);
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<foo, boost::shared_ptr<foo>, boost::noncopyable>(
      "Foo", python::no_init)
    .def("__init__", python::make_constructor(&make_foo))
    .def("__eq__", &foo::operator==)
    .def("__ne__", &foo::operator!=)
    .add_property("value", &foo::get_value, &foo::set_value)
    ;
}

交互式使用:

>>> import example
>>> foo1 = example.Foo("abc")
>>> foo2 = example.Foo("abc")
>>> foo3 = example.Foo("def")
>>> assert(foo1 == foo1)
foo::operator==()
>>> assert(foo1 == foo2)
foo::operator==()
>>> assert(foo1 is not foo2)
>>> assert(foo1 != foo3)
foo::operator!=()
foo::operator==()
>>> foo1.value = foo3.value
>>> assert(foo1 != foo2)
foo::operator!=()
foo::operator==()
>>> assert(foo1 == foo3)
foo::operator==()

从上面的输出中可以看出,C++ 比较运算符是从 Python 调用的。


在该示例中,make_foo() 工厂函数创建唯一的 C++ foo 实例。因此,我选择通过将 make_foo() 包装为构造函数并将其公开为 __init__ 方法,向 Python 隐藏工厂方法的实现细节。如demonstrated here ,如果通过静态方法创建对象,仍然可以检查相等性。另一方面,如果像 get() 这样的静态工厂方法可以返回现有 foo 实例的句柄,那么人们可能会期望相等和身份比较都适用于 >Foo Python 对象(即 assert(Foo.get("abc") is Foo.get("abc")))。要返回对同一 Python 对象的引用,可以需要管理关联的PyObject

关于python - Boost Python 重写相等运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29619799/

相关文章:

c++ - boost::this_thread::sleep_for 的 sleep 时间比我预期的要长得多。

python - QTextBrowser html链接不可点击

Python Web 抓取重定向到其他页面的页面

python - 如何在 Jupyter Notebook 中导入 Pyperclip?

c++ - 为什么遗留 C 标识符不需要 namespace 标准?

c++ - c++ 一段代码中的二进制搜索似乎有问题

python - 如何将 python 脚本放在路径上?

c++ - 按类别对 googletest 单元测试进行分组

c++ - 将 pos_infin 作为超时传递给 timed_wait 时,年份超出有效范围

c++ - 为什么有变异的 Boost.Range 算法的 const 重载?