[New-bugs-announce] [issue43589] Using defaultdict as kwarg to function reuses same dictionary every function call

Tenzin report at bugs.python.org
Mon Mar 22 05:15:29 EDT 2021


New submission from Tenzin <tenzin_chan at mymail.sutd.edu.sg>:

When using a `defaultdict` as a kwarg to a function that requires another argument, every call to the function uses the same dictionary instance instead of creating a new one.

```>>> from collections import defaultdict
>>> def meow(a, b=defaultdict(list)):
...     b[a].append('moo')
...     return b
... 
>>> c = meow('hi')
>>> c
defaultdict(<class 'list'>, {'hi': ['moo']})
>>> c = meow('bye')
>>> c
defaultdict(<class 'list'>, {'hi': ['moo'], 'bye': ['moo']})
>>> d = meow('hello')
>>> d
defaultdict(<class 'list'>, {'hi': ['moo'], 'bye': ['moo'], 'hello': ['moo']})```

Is this the correct behaviour? Occurred in 3.6.12, 3.7.9, 3.8.5, 3.8.6 and 3.9.0.

----------
messages: 389289
nosy: TenzinCHW
priority: normal
severity: normal
status: open
title: Using defaultdict as kwarg to function reuses same dictionary every function call
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list