[C++-sig] __repr__ and string values
Pierre Barbier de Reuille
pierre.barbier at cirad.fr
Thu Apr 29 17:52:11 CEST 2004
That's the expected behaviour !
I'm sure you'll have :
>>> print f.toString()
foo
Python _print_ the string returned by __repr__ or __str__. But if you
return a string it will print repr(string) !
On Thu, 2004-04-29 at 17:41, Dave Berton wrote:
> 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
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
More information about the Cplusplus-sig
mailing list