#include "structmember.h"
Noddy_traverse(Noddy *self, visitproc visit, void *arg)
vret = visit(self->first, arg);
vret = visit(self->last, arg);
Noddy_dealloc(Noddy* self)
self->ob_type->tp_free((PyObject*)self);
Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self = (Noddy *)type->tp_alloc(type, 0);
self->first = PyString_FromString("");
self->last = PyString_FromString("");
Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
PyObject *first=NULL, *last=NULL, *tmp;
static char *kwlist[] = {"first", "last", "number", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
static PyMemberDef Noddy_members[] = {
{"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
{"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
{"number", T_INT, offsetof(Noddy, number), 0,
static PyObject *format = NULL;
format = PyString_FromString("%s %s");
if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first");
if (self->last == NULL) {
PyErr_SetString(PyExc_AttributeError, "last");
args = Py_BuildValue("OO", self->first, self->last);
result = PyString_Format(format, args);
static PyMethodDef Noddy_methods[] = {
{"name", (PyCFunction)Noddy_name, METH_NOARGS,
"Return the name, combining the first and last name"
static PyTypeObject NoddyType = {
"noddy.Noddy", /*tp_name*/
sizeof(Noddy), /*tp_basicsize*/
(destructor)Noddy_dealloc, /*tp_dealloc*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
"Noddy objects", /* tp_doc */
(traverseproc)Noddy_traverse, /* tp_traverse */
(inquiry)Noddy_clear, /* tp_clear */
0, /* tp_weaklistoffset */
Noddy_methods, /* tp_methods */
Noddy_members, /* tp_members */
(initproc)Noddy_init, /* tp_init */
static PyMethodDef module_methods[] = {
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
if (PyType_Ready(&NoddyType) < 0)
m = Py_InitModule3("noddy4", module_methods,
"Example module that creates an extension type.");
PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);