On Sat, Mar 2, 2019 at 6:12 PM Jones, Torrin A (US)
<[email protected]> wrote:
>
> Is it possible to call repr from C++ on a C++ object that can be converted to
> python? Let me explain.
>
>
>
> I have an enum that I converted to python using boost::python::enum_(). That
> class already provides a good repr() to python (thanks to the boost::python
> developers for that). However that enum is used as a type on a member in
> another C++ class that I have where boost::python does not provide a good
> repr(). So I’m trying to provide one and thought, it sure would be nice if I
> could just call repr() on the member. I assume I have to convert the value
> on that member to python first, some kind of way then call repr? Is that
> possible? Or is there a better way?
>
>
>
> Example:
>
>
>
> Typedef enum
>
> {
>
> V1 = 0,
>
> V2 = 5,
>
> V3 = 6
>
> } values;
>
>
>
> Class ValueExample
>
> {
>
> Public:
>
> ValueExample(const values& v) {m_value = v};
>
> Values getValue() const { return m_value };
>
> Private:
>
> Values m_value;
>
> }
>
>
>
> Std::string
>
> ValueExampleRepr(const ValueExample& object)
>
> {
>
> Std::stringstream ss;
>
>
>
> // What do I put here?
>
> // Ss << repr(object.getValue());
>
>
>
> Return ss::str();
>
> }
>
>
>
> Boost::python::enum_<values> values(“values”);
>
> Values.value(“V1”, V1);
>
> Values.value(“V2”, V2);
>
> Values.value(“V3”, V3);
>
>
>
> Boost::python::class_<ValueExample> ValueExample(“ValueExample”);
>
>
>
> ValueExample(“__repr__”, ValueExampleRepr);
I can only come up with this:
Ss <<
extract<std::string>(boost::python::object(object.getValue()).attr("__repr__")())();
_______________________________________________
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig