[C++-sig] returning list of class pointers that compare equal

Gary Oberbrunner garyo at genarts.com
Thu Feb 6 20:43:56 CET 2014



----- Original Message -----
> From: "Jim Bosch" <talljimbo at gmail.com>
...
>  - Add a __eq__/__ne__ overrides to the class of things you're
>  putting
> in the list, with an implementation that compares C++ pointers, not
> Python pointers.  This is almost certainly much easier, but it's a
> little less "complete" as a solution, in the sense that you'll still
> be getting different Python objects back for the same C++ object (so
> while "param2 == param1" will succeed, "param1 is param2" will still
> fail).

This is very helpful!  That's OK about "is" not working, I think (though true, a little odd.)

Can I override __hash__ too, so set membership works correctly?  Also do I need to override __ne__; I think not?

For the record, here's what I did.  Defined a comparison function:

static bool params_equal(Param &left, Param &right)
{
  return &left == &right;
}

and then in the exposer:

    Param_exposer.def("__eq__", &params_equal);


-- 
Gary Oberbrunner


More information about the Cplusplus-sig mailing list