pybind11 - Wrapping overloaded assignment operator?

Viewed 1631

I'm using pybind11 to expose C++ functions to a Python interface. I want to wrap the overloaded assignment operator but don't know how. The documentation and examples provided don't really cover it, at least from what I saw.

Here's a simplified version of what I'm trying to do:

class Num{
  public:
    Num(const double& num) : m_val(num), m_type(Num::Type::e_none){}

    Num& operator=(const double& rhs){m_val = rhs;}

  private:
    double m_val;
};

And here's the wrapping:

PYBIND11_MODULE(demo, m){
    py::class_<Num>(m, "Num")
        .def(py::init<const double&>())
        // Overloaded assignment operator binding ?
        ;
}

My main concern is preserving Num's data type when assigning it to a float. e.g.:

>>> m = Num(4.5)
>>> type(m)
<class 'demo.Num'>
>>> m = 5.5
>>> type(m)
<class 'float'>

This is my first time working with C++ extensions and bindings so any insight on what I should do would be great!

1 Answers
Related