#include "structmember.h"
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, "|SSi", kwlist,
static PyMemberDef Noddy_members[] = {
{"number", T_INT, offsetof(Noddy, number), 0,
Noddy_getfirst(Noddy *self, void *closure)
Noddy_setfirst(Noddy *self, PyObject *value, void *closure)
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
if (! PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
Noddy_getlast(Noddy *self, void *closure)
Noddy_setlast(Noddy *self, PyObject *value, void *closure)
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
if (! PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The last attribute value must be a string");
static PyGetSetDef Noddy_getseters[] = {
(getter)Noddy_getfirst, (setter)Noddy_setfirst,
(getter)Noddy_getlast, (setter)Noddy_setlast,
static PyObject *format = NULL;
format = PyString_FromString("%s %s");
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, /*tp_flags*/
"Noddy objects", /* tp_doc */
0, /* tp_weaklistoffset */
Noddy_methods, /* tp_methods */
Noddy_members, /* tp_members */
Noddy_getseters, /* tp_getset */
(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("noddy3", module_methods,
"Example module that creates an extension type.");
PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);