Wrapping a C++ class.

Jacek Generowicz jacek.generowicz at cern.ch
Tue Feb 25 10:26:42 EST 2003


I am trying to wrap a C++ class, exposing it as a similar-looking
Python class or extension type[*]. One requirement is that the result
be subclassable in Python, anoter is that existing C++ class
hierarchies be reflected in the resulting Python classes. With a major
step having been taken in Python2.2 towards type/class unification,
it's not clear to me whether this is best done using an approach
similar to that shown in

 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/54352

or by using the C++ class as the core of a new Python type, which
should, in Python2.2 and later, be subclassable.

Does the current _Extending and Embedding the Python
Interprer_ document reflect the changes introduced by type/class
unification? (I haven't managed to write an extension type which is an
"acceptable base type", yet.)

While Python seperates the construction of an object into an
allocation step (passing the class as argument to __new__), and an
initialization step (passing the instance created by __new__, to
__init__), in C++ the philosophy is "instantiation is
initialization". Any hints on how the two are best reconciled?

Is there any documentation describing the relation of __new__ and
__init__ to anything in the Python/C API?



[*] (Yes, I am aware of Boost.Python, SWIG, SIP ... the point of the
    exercise is _not_ to wrap the class, but to understand the issues
    involved in the process of doing so when using the Python/C API, so
    please do not tell me to use one of the above; OTOH pointing me to
    part of the source code of any of the above that could be
    instructive (without first having to learn lots of details about
    the implementation of the rest of the system) _would_ be helpful).




More information about the Python-list mailing list