How to create a python class in C

Mark Hammond MarkH at ActiveState.com
Mon Aug 7 07:37:06 EDT 2000


"Uwe Maurer" <uma at webde-ag.de.oz> wrote in message
news:8F897B3C7umawebdeagde at 164.128.36.42...

> can anyone give me some hints how to implement a python class object
in a
> C-extension module. I have already implemented (ok: copied and
altered :))
> a simple object type with some object methods.

It is a PITA to do it all from C code.  You really need your Python
class in a .py file, otherwise it is hard for people to know how to
subclass it!

What you probably want is some sort of delegation mechanism - you can
define your class in .py code, and your .C code work with it.  Your C
code need only hang onto a reference of your instance, and when
necessary, it can use PyObject_Call()/PyObject_GetAttrString() etc to
make a call on the instance (in which case all the normal class
overloading etc happen for you).

Hope this makes sense...

Mark.






More information about the Python-list mailing list