[issue38315] Provide defaultdict variant that passes key to default_factory

Mark Amery report at bugs.python.org
Sun Sep 29 12:12:39 EDT 2019


New submission from Mark Amery <markamery at btinternet.com>:

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.

----------
components: Library (Lib)
messages: 353502
nosy: ExplodingCabbage
priority: normal
severity: normal
status: open
title: Provide defaultdict variant that passes key to default_factory
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38315>
_______________________________________


More information about the Python-bugs-list mailing list