last day (15 days later) » 

5:48 AM
1
A: what is difference using unpack in for loop and not in python

Dillon DavisThe variables u and v store the value of each element of the sublist, without any reference to the sublist itself. When you change either value, it does not cause any side effects. However, e is a reference to the sublist itself. When you index into e and change its values, you are performing an...

 
u and e act as references to the items in the list, the way you've added emphasis might be misleading
 
@juanpa.arrivillaga correct me if I'm wrong, but since u is an int (which is a primitive), it is passed by value rather than reference.
@A.Lee in most languages, simple types like int, float, etc. (called primitives) are treated specially for performance reasons- and so its faster for the language to just copy the value. Other objects are typically passed by reference, for memory and performance reasons, since its slow to copy large objects.
 
@DillonDavis absolutely incorrect. There are no primitive types in Python. Everything is an object, and the evaluation strategy doesn't not change depending on the type of the object. See this question. Suffice it to say, Python is neither call by reference nor call by value. It is "call by sharing", although that is a little used term. This is the same evaluation strategy of most modern languages, e.g. Python, Java, Javascript, Ruby.
But it is very important to understand that whatever you call it, the evaluation strategy does not depend on the type of the object Another good link to read: nedbatchelder.com/text/names.html
 
@juanpa.arrivillaga Python does follow a copy on write strategy, but u and v absolutely do not reference into the list. Just try print(id(0)) and l=[0];for x in l:print(id(x))
 
"Copy on write" is totally irrelevant here. Not sure what you are getting at, nor what you code snippet is supposed to show. Note, small integers are cached, i.e., in Python int objects in the range -2-255 are singletons. That is an implementation detail, though, that shouldn't be relied upon. Anyway, "u and v do not reference into the list", I'm not sure what that's supposed to mean, but u and v are definitely references to the same object in the list.
 
5:48 AM
@juanpa.arrivillaga this snippet shows that 0 and x are in fact the same- x is not a reference. From the docs for id - This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime.
 
And what point are you trying to make? Note, this doesn't work with say, 500, so write a line x = 500, then write l = [500] on another line. The print(id(x)) and print(id(l[0])) (I say write on another line because various optimizations for immutable types may make the interpreter optimize this, usually with int and str literals in the same block)
Again, please please please read the links I've provided to you. Python doesn't have anything even approaching call by reference.
Again, please read the links I've provided. everything in Python acts like a reference to an object, so you might say that "python is call by value where everything is a reference", but that essentially boils down to call by sharing.
Also, read this question with regards to the small integer cache in CPython.
 
I stand corrected. I suppose that's what I get for basing my knowledge of python's inner working off limited experimentation and assumptions from other languages.
I'm torn between deleting my comments as to not confuse others, and leaving them so others can learn from my mistakes. Do you have any suggestion?
 
Eh, the comments shouldn't be the "star" of the questions. I'd just edit to clarify. Honestly, this question is almost certainly a duplicate, I just cant find the right target right now, on my phone
So, I would just change "store the value of each element of the sublist," to "are references to the elements in the list", because otherwise your answer makes sense. The words "value" and "reference" are thrown around loosely, compounding the level of this very common misunderstanding
 
first of all, thank you guys's discussion...really try to understand here..xD
 
I will edit the post as you describe, and remove the comments, since they're still archived here for the curious
 
6:01 AM
ok
@A.Lee the issue isn't where the unpacking is happening. The issue is that in the second case, you merely re-assign the value of u, i.e. u = v = -1 and in the first case, you mutate the list, e[0] = e[1] = -1. Read nedbatchelder.com/text/names.html
 
cool, thank you for clarify..
so lets say if I print the memory location on u and v before the line re-assigning -1 to them. the memory location should be the same as nums[1]? which is the same as e on nums[1]? is my understanding correct?
 

last day (15 days later) »