[C++-sig] __str__ method for wrapper classes

Noah Schwartz noah.schwartz1 at gmail.com
Tue Nov 14 18:17:46 CET 2006


Hi,

I have an inheritance scheme in my C++ library. I expose the base class to 
python using the method described in the tutorial (a struct that inherits 
from my class and from wrapper). I have the extraction operator implemented 
for the base class (lets call the base class MyClass).

I want to be able to print MyClass as a string in python. I 
added .def(str(self)) to the class object for the wrapper (MyClassWrap) 
thinking it would work but, python complains that the __str__ method did not 
match the C++ signature:

Boost.Python.ArgumentError: Python argument types in
    MyClass.__str__(MyClass)
did not match C++ signature:
    __str__(MyClassWrap {lvalue})

I think the problem is that I defined the __str__ method for MyClassWrap and 
not for MyClass. Since MyClassWrap also inherits from wrapper, it can't use 
MyClass's extraction operator. I don't have a class_ object for MyClass. I 
only have it for MyClassWrap and the other classes that inherit from MyClass. 
How do I fix this?

Noag



More information about the Cplusplus-sig mailing list