[C++-sig] Derivation from Python str

Willi Karel wk at ipf.tuwien.ac.at
Thu Sep 30 03:21:32 CEST 2010


Dear Hans and Jim,

thanks a lot for your replies!

I tried to wrap/def boost::python::str's methods for my derived class,
which is possible in a quite compact way using boost::function_types and
some templates. However, I quit my attempt when it came to operators -
too much work for now.

Cheers, Willi.

Jim Bosch wrote:
> On 09/29/2010 09:01 AM, Hans Meine wrote:
>> Am Mittwoch 29 September 2010, 17:02:57 schrieb Willi Karel:
>>> I'd like to derive from either Python's built-in 'str' or
>>> boost::python::str, resulting in a class that provides additional
>>> methods / member functions / attributes. [...]
>>
>> AFAIK this is just an unsupported feature of the BPL.  I was (and
>> would still
>> be) interested in the same thing, only with list/dict being the base
>> classes,
>> but this does not seem to be possible.
>>
>>> From the outside, I would guess/hope that it's not too hard to fix,
>>> but having
>> looked at various parts of boost::python in the past, I don't think
>> that I
>> will try it myself in the near future.
>>
>>> Including e.g. boost::python::str in the list of base classes yields a
>>> run-time error in python: [...]
>>
>> If it was supported, I would say that this would be the right
>> approach/API.
>>
> 
> If you *really* need to subclass Python's str type (so your objects pass
> an "isinstance(obj, str)" check, it is possible to do that in a way that
> is interoperable with Boost.Python (you'd be able to hold such an object
> in boost::python::str, for instance), but you'd have to use a lot of the
> raw Python C API to do it.  It would certainly be harder than just
> adding str-like methods to a regular Boost.Python class.
> 
> Jim Bosch
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig

-- 
                          __ ____ ____   Technische Universität Wien
   DI Wilfried Karel     /_//   // __/  Institut für Photogrammetrie
                        / // / // /_   und Fernerkundung (E122)
 wk at ipf.tuwien.ac.at   / // __// __/  Gußhausstraße 27-29
+43 1 58801 12244     /_//_/  /_/    A-1040 Wien


More information about the Cplusplus-sig mailing list