Added in basic dict attribute/method 'get' which supports default return values or None

This commit is contained in:
Jarius Jenkins 2013-08-26 08:40:39 +09:00
parent 83b3515535
commit f561db1423
1 changed files with 12 additions and 1 deletions

View File

@ -276,7 +276,12 @@ class multi_key_dict(object):
keys.append(key_type(key_val))
return(tuple(keys))
def get(self, key, default=None):
""" Return the value at index specified as key."""
if self.has_key(key):
return self.items_dict[self.__dict__[str(type(key))][key]]
else:
return default
def test_multi_key_dict():
m = multi_key_dict()
@ -456,6 +461,12 @@ def test_multi_key_dict():
# test keys()
assert (m.keys(int) == tst_range), 'm.keys(int) is not as expected.'
# test get functionality of basic dictionaries
m['CanIGet'] = 'yes'
assert (m.get('CanIGet') == 'yes')
assert (m.get('ICantGet') == None)
assert (m.get('ICantGet', "Ok") == "Ok")
print 'All test passed OK!'