# HG changeset patch # User Paul Boddie # Date 1484502246 -3600 # Node ID 336f0d6d1496f416852e434e9130adcafd274860 # Parent 3a12bbe877883aadfd89f854af8f266833644971 Implemented the get method for the dict class. diff -r 3a12bbe87788 -r 336f0d6d1496 lib/__builtins__/dict.py --- a/lib/__builtins__/dict.py Sun Jan 15 18:10:54 2017 +0100 +++ b/lib/__builtins__/dict.py Sun Jan 15 18:44:06 2017 +0100 @@ -151,12 +151,17 @@ def __delitem__(self, key, value): pass - def __getitem__(self, key, default=MISSING): + def __getitem__(self, key): + + "Return the value associated with 'key' from the dictionary." + + return self.get(key, self.MISSING) + + def get(self, key, default=None): """ Return the value stored for 'key'. If 'key' does not have an entry in - the dictionary, a KeyError will be raised unless 'default' is specified. - In which case, 'default' will be returned instead. + the dictionary, 'default' will be returned instead. """ # Find an index identifying the bucket involved. @@ -181,6 +186,7 @@ return self.buckets[index][i][1] def clear(self): pass + def has_key(self): pass def keys(self): @@ -210,7 +216,6 @@ l += bucket return l - def get(self, key): pass def setdefault(self, key, value): pass def update(self, other): pass diff -r 3a12bbe87788 -r 336f0d6d1496 tests/dict.py --- a/tests/dict.py Sun Jan 15 18:10:54 2017 +0100 +++ b/tests/dict.py Sun Jan 15 18:44:06 2017 +0100 @@ -16,6 +16,8 @@ print d[30] # should fail with an exception except KeyError, exc: print "d[30]: key not found", exc.key +print d.get(30) # None +print d.get(30, "c") # c l = f(d) print "# l: ",