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: defaultdict does not support parametric lambda
Type: enhancement Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, xuancong84
Priority: normal Keywords:

Created on 2019-10-09 08:22 by xuancong84, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg354255 - (view) Author: wang xuancong (xuancong84) Date: 2019-10-09 08:22
A very common use of defaultdict is that if the key exist, use the corresponding mapped target, if the key does not exist, use the key itself. However, current Python 2/3 defaultdict does not support parametric lambda function:

>>> from collections import *
>>> aa=defaultdict(lambda t:t)
>>> aa
defaultdict(<function <lambda> at 0x10a55c950>, {})
>>> aa[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() missing 1 required positional argument: 't'
>>>

I would like to suggest that use the dict's query key as the first argument in the default lambda function. And use the dict itself as the 2nd argument in the default lambda function (e.g., if the key exist, use the mapped target, otherwise, use the size of the current defaultdict). I think that will make Python much more powerful than any other programming language. Anyone can think of any additional information for the default lambda function? Thanks!
msg354256 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-09 08:42
All versions below 3.9 are in feature freeze, so they cannot get new features.

This requested feature is already supported by subclassing ``dict`` and giving it a ``__missing__`` method. See issue #38315
History
Date User Action Args
2022-04-11 14:59:21adminsetgithub: 82601
2019-10-09 08:42:27steven.dapranosetstatus: open -> closed

versions: - Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
nosy: + steven.daprano

messages: + msg354256
resolution: rejected
stage: resolved
2019-10-09 08:22:42xuancong84create