diff options
| author | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 +0000 |
|---|---|---|
| committer | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 +0000 |
| commit | ecccdc2966bfd2b9bc6b8016381731fe62c00fe8 (patch) | |
| tree | 51b3a0b4f45eae9931d883f9aa674849b8ed8bd2 /Lib/UserDict.py | |
| parent | 11d2c8fe77ec19a2f1504bbca403dbd2303be0b8 (diff) | |
| download | cpython-ecccdc2966bfd2b9bc6b8016381731fe62c00fe8.tar.gz | |
- Patch 1433928:
- The copy module now "copies" function objects (as atomic objects).
- dict.__getitem__ now looks for a __missing__ hook before raising
KeyError.
- Added a new type, defaultdict, to the collections module.
This uses the new __missing__ hook behavior added to dict (see above).
Diffstat (limited to 'Lib/UserDict.py')
| -rw-r--r-- | Lib/UserDict.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/UserDict.py b/Lib/UserDict.py index 7168703874..5e97817f06 100644 --- a/Lib/UserDict.py +++ b/Lib/UserDict.py @@ -14,7 +14,12 @@ class UserDict: else: return cmp(self.data, dict) def __len__(self): return len(self.data) - def __getitem__(self, key): return self.data[key] + def __getitem__(self, key): + if key in self.data: + return self.data[key] + if hasattr(self.__class__, "__missing__"): + return self.__class__.__missing__(self, key) + raise KeyError(key) def __setitem__(self, key, item): self.data[key] = item def __delitem__(self, key): del self.data[key] def clear(self): self.data.clear() |
