[Python-Dev] getter/setter function signatures

Guido van Rossum guido@python.org
2002年4月18日 10:02:51 -0400


> Looking into Python's sources, I find mostly code like this:
>> static PyObject *
> func_get_code(PyFunctionObject *op)
> {
> ....
> }
>> static PyGetSetDef func_getsetlist[] = {
> {"func_code", (getter)func_get_code, (setter)func_set_code},
> ....
> {NULL} /* Sentinel */
> };
>>>> in other words, casting like hell to the correct function type.
>> Should a style like this should be preferred:
>> static PyObject *
> func_get_code(PyObject *py)
> {
> PyFunctionObject *op = (PyFunctionObject *)py;
> ....
> }
>> static PyGetSetDef func_getsetlist[] = {
> {"func_code", func_get_code, func_set_code},
> ....
> {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/)

AltStyle によって変換されたページ (->オリジナル) /