473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ 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.val ue.__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_v alue, _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\w in32\wintypes.p y", 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 2113
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.val ue.__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_v alue, _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\w in32\wintypes.p y", 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.__s et__() 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
1735
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 elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
5
2782
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
1933
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
2
3604
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 property without actually having to reimplement the get and set methods. In other words, override the the property without changing the functionality of the base property and without explicitly having to reimplement the get and set methods to make it do...
17
2918
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
18
4745
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
10
105199
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
18
6783
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, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
12
6137
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: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
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 we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.