Dear list,
My program needs to do calculation based on a giving data structure (like a sparse matrix) with lots of missing values (invalid indices/keys of lists/dictionaries).
The programing is difficult in that I have to use a try...except block for every item access. I have written a function
def getItem(item, idx, default): try: return item[idx] except: return default
Then I will have to write a[1]['a'][4] as
getItem( getItem( getItem(a,1,{}), 'a', []), 4, 0)
Is there any way to automatically return 0 when any exception is raised for such data acceses? (automatic wrapping a[...] as
try:
a[...]
except:
0
)
Many thanks in advance.
Bo -- http://mail.python.org/mailman/listinfo/python-list