defaultdict factory argument

I think defaultdict would be more useful if one could initialize it with the option to have the key that is being looked up passed to default_factory. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games

On Sat, Jan 17, 2009 at 5:35 PM, Zac Burns <zac256@gmail.com> wrote:
While I don't disagree in principle, giving an example use-case would probably help. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com

For one, it could be used to cache function calls. I'm not sure how well defaultdict and WeakValueDictionary work together (when combined/subclassed), but I can also imagine using a class as the default_factory and using a defaultdict to return a new instance of a class if none yet existed instantiated with the same input parameters or returning the already existing one if one did exist with those parameters. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Sat, Jan 17, 2009 at 5:44 PM, Chris Rebert <pyideas@rebertia.com> wrote:

Continuing the OP's top posting style.. if not hasattr(d,key): d[key] = <long computation> return d[key] or variations, including try:...except:.. work for that Zac Burns wrote:
For one, it could be used to cache function calls.
While I don't disagree in principle, giving an example use-case would probably help.

On Sat, Jan 17, 2009 at 5:35 PM, Zac Burns <zac256@gmail.com> wrote:
Are you sure you can't get what you want by using __missing__? http://www.python.org/doc/2.5.2/lib/defaultdict-objects.html Steve -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy

On Sat, Jan 17, 2009 at 5:35 PM, Zac Burns <zac256@gmail.com> wrote:
While I don't disagree in principle, giving an example use-case would probably help. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com

For one, it could be used to cache function calls. I'm not sure how well defaultdict and WeakValueDictionary work together (when combined/subclassed), but I can also imagine using a class as the default_factory and using a defaultdict to return a new instance of a class if none yet existed instantiated with the same input parameters or returning the already existing one if one did exist with those parameters. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Sat, Jan 17, 2009 at 5:44 PM, Chris Rebert <pyideas@rebertia.com> wrote:

Continuing the OP's top posting style.. if not hasattr(d,key): d[key] = <long computation> return d[key] or variations, including try:...except:.. work for that Zac Burns wrote:
For one, it could be used to cache function calls.
While I don't disagree in principle, giving an example use-case would probably help.

On Sat, Jan 17, 2009 at 5:35 PM, Zac Burns <zac256@gmail.com> wrote:
Are you sure you can't get what you want by using __missing__? http://www.python.org/doc/2.5.2/lib/defaultdict-objects.html Steve -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy
participants (4)
-
Chris Rebert
-
Steven Bethard
-
Terry Reedy
-
Zac Burns