By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,871 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,871 IT Pros & Developers. It's quick & easy.

overloading __getattr__ and inheriting from dict

P: n/a
class TargetWrapper(dict):

def __init__(self, **kwargs):
dict.__init__(self, 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__(self, name):
return dict.__getitem__(self, name)

__getattr__ = __getitem__

then the accessing the b attribute is ok. what's wrong ?
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
BenoƮt Dejean <bn********@ifrance.com> writes:
class TargetWrapper(dict):

def __init__(self, **kwargs):
dict.__init__(self, 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__(self, 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 discussion thread is closed

Replies have been disabled for this discussion.