[C++-sig] __str__ method for wrapper classes

Noah Schwartz noah.schwartz1 at gmail.com
Tue Nov 14 21:08:33 CET 2006


On Tuesday 14 November 2006 12:38, Alex Mohr wrote:
>  From a cursory inspection, I believe this is the same bug as that
> mentioned in this thread:
>
> http://mail.python.org/pipermail/c++-sig/2006-September/011336.html
>
> which David has already fixed in CVS.  Unfortunately, I don't know when
> the next version of boost (and boost python) will be out, so I can't say
> when you'll get it.
>
> As a workaround, I think you can do something like this in your wrapping
> code:
>
> class<...>(...)
>      .def("__str__", boost::lexical_cast<std::string, MyClass const &>)
>
> if MyClass is streamable.  If it's not, you can always do something
> totally custom:
>
> static std::string MyClass__str__(MyClass const &self) {
>      return std::string("Whatever I want.");
> }
>
> class<...>(...)
>      .def("__str__", MyClass__str__)
>
Used the second one. It works nicely.

Thanks.

> Alex
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig



More information about the Cplusplus-sig mailing list