Accessing __slots__ from C

Christian Heimes lists at cheimes.de
Wed Sep 10 11:40:37 EDT 2008


Chris wrote:
> Hi,
> 
> 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.

That's correct. You can use slots to increase the performance but it 
doesn't work like you think. If you need it *really* fast than write a C 
Extension, store the data in a C struct and access the data via PyMemberDef.

Christian




More information about the Python-list mailing list