I have a Python class based on slots to save space
class my_class(object):
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
I need to access objects of this class from a C function. When I print
obj->ob_type->tp_name
from C, I see my_class but can't find a way of accessing the member values on the instance.
For each name in __slots__, Python generates a descriptor object on the class (stored in the PyTypeObject.tp_members struct). Each descriptor stores an offset, just like the __dict__ attribute is stored as an offset.
If you already know the names of the slots and just want to access the values on each instance for those, just use the normal PyObject_GetAttrString() function to leave it to the C-API to access the slots for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With