[Python-Dev] getter/setter function signatures (original) (raw)

Guido van Rossum guido@python.org
Thu, 18 Apr 2002 10:02:51 -0400


Looking into Python's sources, I find mostly code like this:

static PyObject * funcgetcode(PyFunctionObject *op) { .... } static PyGetSetDef funcgetsetlist[] = { {"funccode", (getter)funcgetcode, (setter)funcsetcode}, .... {NULL} /* Sentinel */ };

in other words, casting like hell to the correct function type. Should a style like this should be preferred: static PyObject * funcgetcode(PyObject *py) { PyFunctionObject *op = (PyFunctionObject *)py; .... } static PyGetSetDef funcgetsetlist[] = { {"funccode", funcgetcode, funcsetcode}, .... {NULL} /* Sentinel */ }; in other words, move the cast where you are more sure about it's correctness, and give the compiler a change for meaningfull warnings.

I find that using the first form generally you end up using fewer casts, and that's my preference (because casts obscure the code).

This is one area where I wish we could use C++, so e.g. PyFunctionObject would be a subclass of PyObject... :-(

--Guido van Rossum (home page: http://www.python.org/~guido/)