[Python-Dev] dict.addlist() (original) (raw)

Phillip J. Eby pje at telecommunity.com
Tue Jan 20 10:12:21 EST 2004


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.



More information about the Python-Dev mailing list