Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?
In Python 2.2 and later, you can inherit from builtin classes such as int, list, dict, as well as C types you create yourself.
In earlier versions, C objects are created by factory functions, and cannot be directly inherited. You can get around by using a Python class as a facade, and delegate to an internal C object as necessary.
The Boost Python Library (BPL, http://www.boost.org/libs/python/doc/index.html) provides a way of doing this from C++ (i.e. you can inherit from an extension class written in C++ using the BPL).
CATEGORY: extending
Comment:
With SWIG (www.swig.org) you can automatically generate Python wrappers for C++ classes. If you enable the "director" feature and declare a C++ class as a "director", you can subclass, in Python, the C++ base class, and when in C++ code a virtual function is called that is overridden in Python, it is the Python function that will be called. SWIG does this by generating a C++ class that inherits from the base class, and overrides all virtual methods with methods that by default call the base class C++ implementation, but call into the Python proxy if there is a Python override; this class is the "director". The Python proxy is then generated for the "director" instead of the original C++ base class. I use this feature, and it is both easy to declare, and works like a charm.
Posted by gogins@pipeline.com (2006-11-20)
Comment:
Wouldn't showing a class that has a method in C be a better reply? Like: import cfunc class BigPyClass: def __init__(self): pass compute = cfunc.compute obj = BigPyClass() obj.compute(3) And perhaps showing the code for cfunc..
Posted by Chris Niekel (2006-11-04)