Posts

Showing posts with the label Python C Api

Class Property Using Python C-API

Answer : Similar to these Python solutions, you will have to create a classproperty type in C and implement its tp_descr_get function (which corresponds to __get__ in Python). Then, if you want to use that in a C type, you would have to create an instance of your classproperty type and insert it into dictionary of your type ( tp_dict slot of your type). Follow up: It would seem that it's impossible to set an attribute of a C type. The tp_setattro function of the metaclass ( PyType_Type ) raises a "can't set attributes of built-in/extension type" exception for all non-heap types (types with no Py_TPFLAGS_HEAPTYPE flag). This flag is set for dynamic types. You could make your type dynamic but it might be more work then it's worth. This means that the solution I gave initially allows you to create a property (as in: computed attribute) on a C type object with the limitation that it's read only. For setting you could use a class/static-method ( M...