Re: classes vs. modules vs. types

Guido.van.Rossum@cwi.nl
Tue, 03 May 1994 12:53:11 +0200

> Based upon a couple of the replies I've gotten, it's obvious I was not clear
> in stating my question. I've got a small C++ class hierarchy. I'd like to
> wrap it into Python, not rewrite it in Python. Presumably, if I can learn
> how to incorporate this small class hierarchy into Python I could do it with
> larger, more interesting ones.
>
> Are there any examples out there of people writing Python classes *in C*?
> That would give me some idea of what would be involved when wrapping C++
> classes. Steven Majewski seemed to think nobody has written full-blown
> classes in C.

Well, if you take this literally, that's true. This is because
there's a difference between a "class" and a "type" in Python.
Classes are defined in Python, types in C or C++. (Technically, all
classes are the same Python type.)

It's probably true that nobody has succeeded in defining classes from
C code.

However, wrappers around existing C/C++ classes are written every day.
The documentation for this is in the LaTeX file Doc/ext.tex; examples
are in xxobject.c. It should also be possible to write a small
preprocessor (in Python!) that takes your C++ header files and churns
out wrapper code in C automatically. It may be necessary to mark your
C++ headers up slightly so that the preprocessor doesn't have to be
parse general C++ -- all it needs to know really are the class names,
their public methods, and their argument types.

Since Python's inheritance only works for Python classes, not for
types, it is not possible to inherit directly in Python from a
wrapped-up C++ class; however question 4.2 in the Python FAQ shows a
way around this.

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>