New submission from Mark Amery <markam...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue38315>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to