trap attributeError from inside a dict ?

GrelEns grelens at NOSPAMyahoo.NOTNEEDEDfr
Wed Mar 10 03:27:48 EST 2004


hello,

i would like if this behaviour can be obtained from python : trap an
attributeError from inside a subclassing dict class... (here is a silly
examples to explain my question)

class Test(dict):
 """subclassing a dict and each key will permit access to a tuple of fized
size"""
 def __init__(self):
  pass
 def load(self, adict):
  """actually it will be done from a text file with fixed size fields, each
tuple will have the same size"""
  for k in adict:
   self[k] = tuple(adict[k])

t = Test()
t.load({1: ('a', 'aa'), 2: ('(b', 'bb')})

here is what i would have :
>>> t[1].get('first')
'a'

without having to define tuple as an object or subclassing tuple, of course
now i had :
>>> t[1].get('first')

Traceback (most recent call last):
  File "<pyshell#15>", line 1, in -toplevel-
    t['A'].get('first')
AttributeError: 'tuple' object has no attribute 'get'

is it possible ?

btw my question is something like is there a way from inside Test code to
say to python that when it first get t[1] it should not go further, trap the
things one ask to do on t[key] and then calling a function such as

(inside Test)
    def __get(self, key, readable):
        mapping = {'first' : 1, 'second' : 2}
        return self[key][mapping[readable]]

(maybe it is a silly design...but i need this because i do want some kind of
readable object access without having to create all these objects)





More information about the Python-list mailing list