[Python-Dev] Proposal: defaultdict (original) (raw)
Adam Olsen rhamph at gmail.com
Fri Feb 17 22:54:08 CET 2006
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2/17/06, Guido van Rossum <guido at python.org> wrote:
- There's a fundamental difference between associating the default value with the dict object, and associating it with the call. So proposals to invent a better name/signature for setdefault() don't compete.
That's a feature, not a bug. :) See below.
- The inconsistency introduced by getitem() returning a value for keys while get(), contains(), and keys() etc. don't show it, cannot be resolved usefully. You'll just have to live with it. Modifying get() to do the same thing as getitem() doesn't seem useful -- it just takes away a potentially useful operation.
Again, see below.
So here's a new proposal.
Let's add a generic missing-key handling method to the dict class, as well as a defaultfactory slot initialized to None. The implementation is like this (but in C): def onmissing(self, key): if self.defaultfactory is not None: value = self.defaultfactory() self[key] = value return value raise KeyError(key) When getitem() (and only getitem()) finds that the requested key is not present in the dict, it calls self.onmissing(key) and returns whatever it returns -- or raises whatever it raises. getitem() doesn't need to raise KeyError any more, that's done by onmissing().
Still -1. It's better, but it violates the principle of encapsulation by mixing how-you-use-it state with what-it-stores state. In doing that it has the potential to break an API documented as accepting a dict. Code that expects d[key] to raise an exception (and catches the resulting KeyError) will now silently "succeed". I believe that necessitates a PEP to document it.
It's also makes it harder to read code. You may expect d[key] to raise an exception, but it won't because of a single line up several pages (or in another file entierly!)
d.getorset(key, func) has no such problems and has a much simpler specification:
def getorset(self, key, func): try: return self[key] except KeyError: value = self[key] = func() return value
-- Adam Olsen, aka Rhamphoryncus
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]