2

I am trying to get the deepcopy of a dict and update the result in Python 2.6. The following code works well:

>>> a = {1:2}
>>> b = copy.deepcopy(a)
>>> b.update({3:4})
>>> b
{1: 2, 3: 4}

while the following code does not work

>>> a={1:2}
>>> b = copy.deepcopy(a).update({3:4})
>>> b
>>> 

Why the second code snippet gives None?

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
user1659464
  • 313
  • 1
  • 3
  • 9

2 Answers2

3

dict.update is an inplace operation like list.sort, it does not return a value it modifies the original object it is called on.

If you actually print b you would see None as all python functions that do not specify a return value will return None by default. So you are assigning b to the result of calling update which is actually None.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Your update method actually returns None. update() method output argument is actually an input dict you're providing, which is mutated inside a function.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93