Accessing __slots__ from C

Hrvoje Niksic hniksic at xemacs.org
Thu Sep 11 07:33:01 EDT 2008


[ You can use the capi-sig for questions like this; see
  http://mail.python.org/mailman/listinfo/capi-sig ]

Chris <ceball at users.sourceforge.net> writes:
> I'd like to be able to access an attribute of a particular Python
> object as fast as possible from some C code.
>
> I wondered if using __slots__ to store the attribute would allow me to
> do this in a faster way.
>
> The reason I'd like to do this is because I need to access the
> attribute inside a loop within some C code, and I find that the
> attribute lookup using the 'PyObject_GetAttrString' call is far slower
> than any of the subsequent calculations I perform in C.

PyObject_GetAttrString is convenient, but it creates a Python string
only so it can intern it (and in most cases throw away the freshly
created version).  For maximum efficiency, pre-create the string
object using PyString_InternFromString, and use that with
PyObject_GetAttr.



More information about the Python-list mailing list