Long time ago a thought came, why does a python dict throw exception, can't it just return a None if there is no key with supplied name.
Well now I understand the point of it, which is very obvious - if a dict has a key "foo" and the programmer has stored None into it, how would one distinguish a non-existant key and the one with None stored into it.
So here is the desired bad extension of dict for the Community Centric Python(CCP) Engineer:
class NoneDict(dict):
def __getitem__(self,key):
try:
return self.get(key)
except:
return None
l = NoneDict()
l[1] = "Foo"
l["a"] = "Bar"
print l[1], l["a"] # gives out the values
print l[42] # gives out None
Labels: ccp, fail, python