473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading __getattr__ and inheriting from dict

class TargetWrapper(d ict):

def __init__(self, **kwargs):
dict.__init__(s elf, kwargs)

__getattr__ = dict.__getitem_ _
__setattr__ = dict.__setitem_ _
__delattr__ = dict.__delitem_ _

then

tw = TargetWrapper()
tw.a = "spam" # ok
del tw.a # ok
tw.b = "egg"
print tw.b

last line give me an
AttributeError: 'TargetWrapper' object has no attribute 'b'

if i define

def __getitem__(sel f, name):
return dict.__getitem_ _(self, name)

__getattr__ = __getitem__

then the accessing the b attribute is ok. what's wrong ?
Jul 18 '05 #1
1 2059
Benoît Dejean <bn********@ifr ance.com> writes:
class TargetWrapper(d ict):

def __init__(self, **kwargs):
dict.__init__(s elf, kwargs)

__getattr__ = dict.__getitem_ _
__setattr__ = dict.__setitem_ _
__delattr__ = dict.__delitem_ _

then

tw = TargetWrapper()
tw.a = "spam" # ok
del tw.a # ok
tw.b = "egg"
print tw.b

last line give me an
AttributeError: 'TargetWrapper' object has no attribute 'b'

if i define

def __getitem__(sel f, name):
return dict.__getitem_ _(self, name)

__getattr__ = __getitem__

then the accessing the b attribute is ok. what's wrong ?


I'm not sure, but changing

__getattr__ = dict.__getitem_ _

to

__getattribute_ _ = dict.__getitem_ _

makes things behave more like I think you expect.

Cheers,
mwh

--
Of course, it obviously is beta hardware so such things are to be
expected, but that doesn't mean that you can't point your fingers
and generate a nelson style HAHA at a multi billion dollar
corporation's expense. -- CmdrTaco on slashdot.org
Jul 18 '05 #2

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

Similar topics

1
2039
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into inheritance and operator overloading (both concepts that I initially found hard to grasp). I've written a simple config file parser - and I thought I'd experiment with making the interface easier by subclassing dict and overloading the __setitem__,...
0
2152
by: Christian Hudon | last post by:
Hi, I'd like to be able to trap lookups of attributes of the current module. Exactly like what __getattr__ does, but for modules. As an exmaple, I'd like to be able to do: File foo.py enable_magic()
13
2008
by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the rigth hand side of < did not work. Expanded version: In a class of numbers I have been able to overload the four arithmetic operators +,-,* and / by defining the methods __add__ and __radd__ (etc..) in my class. This enables me to evaluate...
2
5496
by: Peter Bengtsson | last post by:
Hi, I'm trying to pickle an object instance of a class that is like a dict but with a __getattr__ and I'm getting pickling errors. This works but is not good enough. $ python2.4 .... pass .... {'age': 40, 'name': 'Zahid'} {'age': 40, 'name': 'Zahid'}
0
10069
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
9735
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...
0
8736
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6556
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
5168
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...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
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
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.