In Python 3, if I create a dict from dict.fromkeys
then it displays a different behavior than just assigning a dict to a variable. I could see this when I used extend
on both dicts.
dict_a = dict.fromkeys(['x', 'y', 'z'], [])
dict_b = {'x': [], 'y': [], 'z': []}
dict_a == dict_b # returns True
type(dict_a) == type(dict_b) # returns True
def test_dicts(d):
d['z'].extend([1, 2, 3])
print(d)
test_dicts(dict_a)
returns
{'x': [1, 2, 3], 'y': [1, 2, 3], 'z': [1, 2, 3]}
test_dicts(dict_b)
returns
{'x': [], 'y': [], 'z': [1, 2, 3]}
Is this expected behavior? If so, why?