Python assignment in for loops (theory clarification)


Python assignment in for loops (theory clarification)



I need to clarify what is going here, and what is the real reason for it:


array = [0, 1, 2]
for element in array:
element += 1

print array #CHANGES NOTHING

double_array = [[0,1], [2,3]]
for element in double_array:
element[0] = "Changed!"

print double_array #THIS WORKS

for element in double_array:
element = ["Doesn't work!"]

print double_array #NOPE



I understand this intuitively, but I'm not sure about the theory behind this. The way I feel is that the variable name in python is this ethereal thing that immediately falls apart if you put "=" directly after it. If the variable refers to a list, you can use ".append" after it, or "[0] =" after it, and it acts like a reference in C, but if you assign something to it directly, python is immediately like, "oh you don't need this name anymore? Ok, now it just refers to this thing you assigned to it, and nothing else".



In particular, it seems wrong to me to say regarding my first case that "integers are immutable". In my third case, a list appears to be just as immutable as the integer. What is the proper explanation for this?





Variables in Python are just "names" for objects. Assigning to a name changes what that name refers to; it does nothing to the old object that name used to refer to. element[0] = actually calls a method on the list object, which is why it is mutated.
– Jonathon Reinhart
Jun 30 at 12:56



element[0] =





Is "[0]" the method or is "[0] =" the method? In my own classes I can't have a method that returns a reference to which I can assign something.
– Jānis Avotiņš
Jun 30 at 13:07




2 Answers
2



In Python, every variable, array element, and attribute is a reference, and the only thing =, +=, -= etc. are able to do is to change what its left-hand side refers to. Note that any other operation will act directly on the value that is being referenced, such as . (look up attribute of the object that is referenced by the left-hand side) or (look up element of the list/dict that is referenced by the left-hand side).


=


+=


-=


.




If you are trying to understand Python in terms of C, keep this in mind:


a.b


a->b


(*a).b



In first case pyhton makes a new variable for each value in list and does not apply old reference to variable. But in second Case python makes a new variable for list as a whole but not for each varibale. So, reference of elements in list remain same and changed that paricular element. In third Case it create a variable like second case and changed variable as a whole but not value of each element.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter