Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is GetSetDescriptorType in Python?

Tags:

python

I was looking at types.py to understand the built-in types and I came across this GetSetDescriptorType. From the Python documentation:

types.GetSetDescriptorType

The type of objects defined in extension modules with PyGetSetDef, such as FrameType.f_locals or array.array.typecode. This type is used as descriptor for object attributes; it has the same purpose as the property type, but for classes defined in extension modules

I do understand the property type, but could not wrap my mind around this. Can some one who understands this throw some light ?

like image 280
Thava Avatar asked Feb 06 '26 02:02

Thava


1 Answers

When you write a Python module using C, you define new types using a C API. This API has a lot of functions and structs to specify all the behavior of the new type.

One way to specify properties of a type using the C API is to define an array of PyGetSetDef structs:

static PyGetSetDef my_props[] = { /*... */ }

And then use the array in the initialization of the type (see this example for details).

Then, in Python, when you use MyType.my_property you have a value of types.GetSetDescriptorType, that is used to resolve the actual value of the property when you write my_obj.my_property.

As such, this type is an implementation detail, unlikely to be very useful.

like image 153
rodrigo Avatar answered Feb 07 '26 15:02

rodrigo