EDIT:
Why does [defaultdict(int)] * 3
return three references to the same object?
Original Title
Unpack list of defaultdicts into variables has unexpected behavior in Python
Unpacking an initialized list of defaultdict
types into variables does not appear to work the way I would expect it to. Does anyone know why this behaves this way (see code snippets below)? I'm using using Python 3.9.1
.
# Equivalent behavior - works OK
a,b,c = [int(), int(), int()]
d,e,f = [int()] * 3
# Expected equivalent behavior - BEHAVES DIFFERENTLY
l,m,p = [defaultdict(int), defaultdict(int), defaultdict(int)]
q,r,s = [defaultdict(int)] * 3
Full snippet:
>>> a,b,c = [int(), int(), int()]
>>> a+=4; b+=2; c+=7
>>> a,b,c
(4, 2, 7)
>>> d,e,f = [int()] * 3
>>> d+=11; e+=8; f+= 41
>>> d,e,f
(11, 8, 41)
>>> from collections import defaultdict
>>> l,m,p = [defaultdict(int), defaultdict(int), defaultdict(int)]
>>> l['a']+=1; m['b']+=2; m['c']+=3;
>>> l,m,p
(
defaultdict(<class 'int'>, {'a': 1}),
defaultdict(<class 'int'>, {'b': 2, 'c': 3}),
defaultdict(<class 'int'>, {})
)
>>> q,r,s = [defaultdict(int)] * 3
>>> q['a']+=111; r['b']+=222; m['c']+=333;
>>> q,r,s
(
defaultdict(<class 'int'>, {'a': 111, 'b': 222}),
defaultdict(<class 'int'>, {'a': 111, 'b': 222}),
defaultdict(<class 'int'>, {'a': 111, 'b': 222})
)
This question is based on the topic posed by the question "Unpack list to variables".