Quoting skip@pobox.com: > > Guido> Over lunch with Alex Martelli, he proposed that a subclass of > Guido> dict with this behavior (but implemented in C) would be a good > Guido> addition to the language. > > Instead, why not define setdefault() the way it should have been done in the > first place? When you create a dict it has the current behavior. If you > then call its setdefault() method that becomes the default value for missing > keys. > > d = {'a': 1}' > d['b'] # raises KeyError > d.get('c') # evaluates to None > d.setdefault(42) > d['b'] # evaluates to 42 > d.get('c') # evaluates to 42 > > For symmetry, setdefault() should probably be undoable: deldefault(), > removedefault(), nodefault(), default_free(), whatever. Well, first not ot break the current interface, and second because I think it reads better I would prefer : d = {'a': 1}' d['b'] # raises KeyError d.get('c') # evaluates to None d.default = 42 d['b'] # evaluates to 42 d.get('c') # evaluates to 42 And to undo the default, you can simply do : del d.default And of course, you can get the current value : d.default But then, as proposed many times, I would rather see a function call. Like : d.default = lambda key: 42 The argument of the function is the current key. It would allow things like that : d.default = time_comsuming_operation where time_comsuming_operation get a single argument. > > The only question in my mind is whether or not getting a non-existent value > under the influence of a given default value should stick that value in the > dictionary or not. > > down-with-more-builtins-ly, y'rs, > > Skip > _______________________________________________ > Python-Dev mailing list > Python-Dev@python.org > http://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > http://mail.python.org/mailman/options/python-dev/pierre.barbier%40cirad.fr >