Creating class instances in C

Andrew KUCHLING (fnord@cs.mcgill.ca)
22 Jun 1994 20:14:43 GMT

I'm writing an extension module to interface Python with a complicated
API written in C. Writing methods that return fundamental Python
objects such as integers and strings is quite simple to do, and
presents no problems. (My compliments to Guido for making Python so
easily extensible.)

However, certain API functions return large C structures, which I'd
like to return as a Python class instance containing various
attributes. Is there any way for an extension module written in C to
find the class "Item", create an instance of it, and then add various
attribute variables to the new instance?

One alternative I've considered is to create a dictionary with various
key/value pairs; Python wrappers around the C code could then create a
new class instance and add the values in the dictionary. I'll do that
if creating a class instance in C is impractical or difficult, but
that'll be a *lot* of wrapper functions. I could just return the
dictionary, but I want the resulting Python code to resemble the old C
code for the API as much as possible, so I'd really prefer to use the
'.' syntax for referencing attributes.

So, does anyone have a suggestion as to how this should be
implemented? Thanks in advance...

Andrew Kuchling
fnord@binkley.cs.mcgill.ca