[C++-sig] Derivation from Python str
talljimbo at gmail.com
Wed Sep 29 22:14:43 CEST 2010
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.
More information about the Cplusplus-sig