You can do s.get(6,None)
On Oct 25, 1:07 pm, pepper_bg <ilief...@gmail.com> wrote: > So gluon.storage.Storage is an amended dict subclass with dot notation > indexing added. With the additional twist that when a key is missing > s['missing_key'] will still raise the standard dict KeyError, while > s.missing_key will return None. All this is fine, only as long as your > keys are strings though. Using integer keys is still a very legitimate > dict/Storage use, but you just don't have the non-KeyError-raising > functionality (s.4 is just invalid syntax), i.e. I have to write > different code depending on what my keys are. > > Here is some code if my explanation isn't clear: > > >>> from gluon.storage import Storage > >>> s=Storage({'id':12,5:'yesterday'}) > > #check for a missing string key>>> s.max #None > >>> s['max'] > > Traceback (most recent call last): > File "<console>", line 1, in <module> > KeyError: 'max' > > #check for a missing integer key>>> s[6] > > Traceback (most recent call last): > File "<console>", line 1, in <module> > KeyError: 6 > > >>> s.6 > > File "<console>", line 1 > s.6 > ^ > SyntaxError: invalid syntax > > > > I thought it would be trivial to make s['missing_key'] return None, > just like for example gluon.storage.List already does. Or was the > intent whoever wanted to catch exceptions to use s['missing_key'] and > whoever wanted to check for None to use s.missing_key? If that is the > case I guess a lot of legacy code exists which already relies on > catching exceptions and I will stick to my Storage subclass but wanted > to check just in case this behavior could be changed sometime in > gluon.storage. > > Appreciate your feedback...