0

Why assignment in python is dynamic??

hi, consider below: a=[1,2,3] b=a a[0]="test" print(b) output is: >>>["test",2,3] why the value of "b" changed dynamically with the change in variable "a"?? this a bit awkward if you are coming from C language background

7th Jun 2018, 2:44 AM
Mugahed Izzeldin Osman
Mugahed Izzeldin Osman - avatar
3 Answers
+ 2
It's the same thing in C, with nothing to do with dynamicity. You are assigning the reference of a to b. The variable b refers to the same object as a. Coming from a C background shouldn't make this look any awkward. Instead, it should make more sense. Read into the differences between shallow copy and deep copy. https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy
7th Jun 2018, 4:07 AM
Fermi
Fermi - avatar
7th Jun 2018, 2:04 PM
Fermi
Fermi - avatar
0
nice, thanks Fermi , I guess the right question is how to perform deep copy with python?? :)
7th Jun 2018, 9:36 AM
Mugahed Izzeldin Osman
Mugahed Izzeldin Osman - avatar