Python: unpacking into array elements -
why behavior of unpacking change when try make destination array element?
>>> def foobar(): return (1,2) >>> a,b = foobar() >>> (a,b) (1, 2) >>> = b = [0, 0] # make , b lists >>> a[0], b[0] = foobar() >>> (a, b) ([2, 0], [2, 0])
in first case, behavior expect. in second case, both assignments use last value in tuple returned (i.e. '2'). why?
when a = b = [0, 0]
, you're making both a
, b
point same list. because mutable, if change either, change both. use instead:
a, b = [0, 0], [0, 0]
Comments
Post a Comment