[C++-sig] __repr__ and string values
Dave Berton
db at nec-labs.com
Thu Apr 29 17:41:27 CEST 2004
Consider a class which can represent itself as a string:
class Foo
{
public:
std::string toString() { return "foo"; }
};
This is wrapped thus:
class_< Foo, boost::shared_ptr<Foo> >("Foo", "The Foo class" )
.def(init< >())
.def("toString", &Foo::toString )
.def("__repr__", &Foo::toString )
;
When imported into python, I get different return values calling
Foo::toString() compared to when I let python get the return value
implicitly:
>>> from foo_ext import *
>>> f = Foo()
>>> f.toString()
'foo'
>>> f
foo
Why does the explicit call to Foo::toString() return a properly quoted
string while the implicit call invoked by python does not? How should I
wrap Foo such that both ways of getting the string representation return
the same thing (a quoted string)?
db
More information about the Cplusplus-sig
mailing list