This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Provide defaultdict variant that passes key to default_factory
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ExplodingCabbage, rhettinger, xuancong84
Priority: normal Keywords:

Created on 2019-09-29 16:12 by ExplodingCabbage, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg353502 - (view) Author: Mark Amery (ExplodingCabbage) * Date: 2019-09-29 16:12
There seems to be at least some demand for the ability to have the key being accessed be passed to the factory function of a defaultdict. See e.g. https://stackoverflow.com/q/2912231/1709587 (13k views) or previous enhancement suggestion https://bugs.python.org/issue30408.

However, as noted on that issue, defaultdict cannot simply be modified to pass the key to the factory function without breaking backwards compatibility.

Perhaps it makes sense, then, to have a separate class in the collections module, perhaps called keydefaultdict, that is like defaultdict but whose factory function receives one argument, which is the key being accessed? Having this built into the standard library would be slightly more convenient than either making an ad-hoc dict subclass every time this functionality is needed or having to roll your own subclass like the one at https://stackoverflow.com/a/2912455/1709587.
msg353504 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-09-29 16:49
The __missing__() method already fulfills this need:

   class DoubleDict(dict):

       def __missing__(self, key):
           return key * 2

If desired, it can also add entries:

   class DoubleDict(dict):

       def __missing__(self, key):
           self[key] = value = key * 2
           return value
msg353576 - (view) Author: Mark Amery (ExplodingCabbage) * Date: 2019-09-30 14:32
I'm aware of __missing__. However, I don't think its existence is by itself a knockdown argument against doing this. __missing__ also fulfils the need satisfied by defaultdict, yet Python still has both - and the latter is almost always more convenient to use when it's possible to do so.
msg354257 - (view) Author: wang xuancong (xuancong84) Date: 2019-10-09 08:49
I agree with Mark Amery.
The reason why defaultdict still exists given that everything can be achieved by subclassing the built-in dict is because of convenience. I suggest maybe Python developer can put it into low priority instead.
History
Date User Action Args
2022-04-11 14:59:21adminsetgithub: 82496
2019-10-09 08:49:23xuancong84setnosy: + xuancong84
messages: + msg354257
2019-09-30 14:32:32ExplodingCabbagesetmessages: + msg353576
2019-09-29 16:49:50rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg353504

resolution: rejected
stage: resolved
2019-09-29 16:12:39ExplodingCabbagecreate