At 09:56 AM 1/20/04 -0500, Phillip J. Eby wrote: >try: > return self._somemapping[key] >except: > self._somemapping[key] = value = somethingExpensive(key) > return value Oops, that should've been except KeyError. Which actually points up another reason to want to use a setdefault mechanism, although admittedly not for the builtin dictionary type. Using exceptions for control flow can mask actual errors occuring within a component being used. Thus, when I create mapping-like objects, I try as much as possible to push "defaulting" into the lower-level mappings, to avoid needing to trap errors and turn them into defaults. Sometimes, however, that default is expensive to create, so I actually use a factory argument, and it's often named 'factory'. Hence, my enthusiasm for the suggestion.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4