[C++-sig] Boost.Python and std::string subclassing

Roman Yakovenko roman.yakovenko at gmail.com
Mon May 5 20:12:34 CEST 2008


On Mon, May 5, 2008 at 3:56 PM, Pertti Kellomäki
<pertti.kellomaki at tut.fi> wrote:
> The library I am wrapping creates a subclass TCEString of std::string.
>  Py++ generates me bindings for the subclass, but in Python the
>  subclass does not quite work like std::string. For example,
>  the Python expression
>
>  "%s.%s.%s" % (term.functionUnit().name(), term.port().name(),
> term.operation().name())
>
>  returns the string
>
>  "universal_fu.OCSetting32.<TCE.tools.TCEString object at 0x81308ec>"
>
>  because term.operation().name() returns a TCEString instead of std::string.
>
>  Is there some convenient way to make the subclass work like std::string?
>  I know how to do it by defining a __str__ method in the Python class,
>  but I am hoping that there might be a simpler way to do it using
>  Boost.Python or Py++.

I am not sure I fully understand you.

Do you want to add new method "__str__" to TCEString ? If so take a
look on this document:
http://language-binding.net/pyplusplus/documentation/inserting_code.html

If you want to "hide" from user class TCEString and to define your
interface in terms of Python string than:
1. create custom converter (
http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html#custom_string
)
2. In Py++ script mark TCEString classs as "already exposed", so you
will not get warnings. See
http://language-binding.net/pyplusplus/documentation/multi_module_development.html#already-exposed
for an explanation.

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list