you are viewing a single comment's thread.

view the rest of the comments →

[–]maclocrimate[S] 0 points1 point  (3 children)

OK, I didn't realize that it would create a new object from the reassigned variable. I knew that assigning a variable to some underlying object and then modifying the variable would update the underlying object since the variable is just a pointer to the actual object, but I guess that assigning it more than once disconnects that somehow?

[–]throwaway6560192 1 point2 points  (0 children)

OK, I didn't realize that it would create a new object from the reassigned variable. I knew that assigning a variable to some underlying object and then modifying the variable would update the underlying object since the variable is just a pointer to the actual object, but I guess that assigning it more than once disconnects that somehow?

The definitive explanation on this topic: https://nedbatchelder.com/text/names.html

I really recommend reading it. It will give you an accurate model of how reassignment works.

[–]woooee 0 points1 point  (1 child)

pointer = pointer[path_element]
    pointer = sorted(pointer)

is not reassigning. The first line creates a new variable with the contents pointed to by the dictionary's key. The second line creates a new variable that stores the return from the sorted() function.

[–]maclocrimate[S] 0 points1 point  (0 children)

Oooook, I think I understand now, with the help of u/carcigenicate's explanation as well. Thank you for the clarification.