Overriding tp_getset attribute in subclasses

Thomas Heller theller at python.net
Wed Aug 18 13:51:13 CEST 2004


I have a subclassable type implemented in C, which has a 'value'
attribute implemented in the tp_getset slot.  The type is named c_long.
The value attribute accepts and returns integers.

Now I want to derive a subclass 'BOOL' (in Python) from it, where the
'value' attribute should accept and return bool instances:

from ctypes import c_long

class BOOL(c_long):
    def _get_value(self):
        return bool(c_long.value.__get__(self))
        # this also works:
        # return bool(super(BOOL, self).value)

    def _set_value(self, val):
        c_long.value.__set__(self, val)
        # this does not work:
        # super(BOOL, self).value = val

    value = property(_get_value, _set_value)

I had expected the commented out super() calls to also do the work, but
at least in the _set_value method it does not work: 

  File "c:\sf\ctypes\win32\wintypes.py", line 37, in _set_value
    super(BOOL, self).value = val
TypeError: 'super' object has only read-only attributes (assign to .value)

The c_long.value.__get__ and c_long.value.__set__ variants work, but
they look strange - is there a better way?

Thanks,


Thomas



More information about the Python-list mailing list