[Python-Dev] defaultdict proposal round three (original) (raw)
Guido van Rossum guido at python.org
Tue Feb 21 00:23:36 CET 2006
- Previous message: [Python-Dev] defaultdict proposal round three
- Next message: [Python-Dev] defaultdict proposal round three
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2/20/06, Dan Gass <dan.gass at gmail.com> wrote:
Why not have the factory function take the key being looked up as an argument?
This was considered and rejected already.
You can already customize based on the key by overriding on_missing() [*]. If the factory were to take a key argument, we couldn't use list or int as the factory function; we'd have to write lambda key: list(). There aren't that many use cases for having the factory function depend on the key anyway; it's mostly on_missing() that needs the key so it can insert the new value into the dict.
[*] Earlier in this thread I wrote that on_missing() could be inlined. I take that back; I think it's better to have it be available explicitly so you can override it without having to override getitem(). This is faster, assuming most getitem() calls find the key already inserted, and reduces the amount of code you have to write to customize the behavior; it also reduces worries about how to call the superclass getitem method (catching KeyError might catch an unrelated KeyError caused by a bug in the key's hash or eq method).
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] defaultdict proposal round three
- Next message: [Python-Dev] defaultdict proposal round three
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]