[New-bugs-announce] [issue38420] defaultdict does not support parametric lambda

wang xuancong report at bugs.python.org
Wed Oct 9 04:22:42 EDT 2019


New submission from wang xuancong <xuancong84 at gmail.com>:

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!

----------
messages: 354255
nosy: xuancong84
priority: normal
severity: normal
status: open
title: defaultdict does not support parametric lambda
type: enhancement
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

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


More information about the New-bugs-announce mailing list