473,465 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overriding tp_getset attribute in subclasses

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
Jul 18 '05 #1
1 2098
Thomas Heller <th*****@python.net> writes:
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)
A bug on SF just like this just got closed, and this text added to the
documentation:

Note that \function{super} is implemented as part of the binding
process for explicit dotted attribute lookups such as \samp{super(C,
self).__getitem__(name)}. Accordingly, \function{super} is
undefined for implicit lookups using statements or operators such as
\samp{super(C, self)[name]}.

So you should write

super(BOOL, self).__setattr__('value', val)

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


I guess super(BOOL, BOOL).value.__set__() might work too... haven't
tried it, though.

I also note that the documentation for super() starts off with

Return the superclass of \var{type}.

which is wrong, and that super() is documented in libfuncs.tex, which
is also wrong. Some bug reports are coming up!

Cheers,
mwh

--
GAG: I think this is perfectly normal behaviour for a Vogon. ...
VOGON: That is exactly what you always say.
GAG: Well, I think that is probably perfectly normal behaviour for a
psychiatrist. -- The Hitch-Hikers Guide to the Galaxy, Episode 9
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dietmar Gräbner | last post by:
Hi Right now I'm dealing with derivation by restriction and I have some questions concerning the attribute property use in context of the derivation. Consider following Schema: <xs:schema...
5
by: zero | last post by:
I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code: class BaseClass { protected: void method2(); public: void method1();
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
2
by: ESPNSTI | last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month. My experience has been mainly with Delphi 5 (not .Net). What I'm looking for is for a shortcut way to override a...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.