Help needed adding attributes to a C-coded class

vincent wehren vincent at
Sun Dec 14 18:59:39 CET 2003

Hi folks,

How can I add an attribute to a C-coded class (..using PyClass_New()...) in
the __init__ method of that class, making it show up in "self.__dict__" on
the Python side?

IOW, when I have a class called "Screen" in a C-extension that has an
__init__ method that takes a single argument called "name" (apart from
 "self" of course), how can I add this as an attribute to the class so that
it equals a " = name" assigment in a pure Python class?

So far my "__init__" method looks something like:

static PyObject *

ex_Screen__init__(PyObject *self, PyObject *args)


    char *name;

    PyObject *selfObject;

    if (!PyArg_ParseTuple(args, "Os", &selfObject, &name))

        return NULL;

    /*now "name" becomes one of the instances attributes

      exposed as instance.__dict__ on Python side..... but how?*/


    return Py_None;


Any pointers much appreciated!


Vincent Wehren

More information about the Python-list mailing list