Re: Need help wrting classes/methods in C

Guido.van.Rossum@cwi.nl
Sat, 18 Jun 1994 11:32:50 +0200

> I can successfully build functions in C, and have them built and
> imported. This works fine.
>
> But now, I want to write _methods_ in C, and have some questions.
>
> * Should I define the class in Python before importing my module, or
> should I define the class itself in 'C'. If in 'C', how do I do this?
> Are there any samples of this?
>
> * Once I have my class defined, how do I attach a C function to the
> class as a method? The inittab[] is the link from function<->module, but
> I cant see anything too similar for classes.
>
> * Can I define attributes/member vars/whatever you call them as C
> variables, rather than Python objects. ie, can I put a native (say)
> pointer as a member variable (only to be accessed by the C code, of
> course)

Mark,

You cannot wuse C (or C++) to write methods for classes defined in
Python directly, nor can you define a Python class in C/C++. Maybe
question 4.2 from the FAQ (ftp://ftp.cwi.nl/pub/python/python-FAQ) is
also relevant.

Maybe what you want is to define new *types* in C. This is entirely
possible (see e.g. Objects/xxobject.c). Also see the structmember.h
file (and its implementation, structmember.c) -- this lets you define
an easy mapping between members of a C structure and attributes of a
Python object.

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