View Single Post
Old Dec 8th, 2006, 3:58 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich View Post
In other words, the variable 'a' in the function stays local to the function, as long as variable 'a' is immutable like a number, string or tuple. Immutable parameters are passed by value.
No, that's not it. The only difference between a normal object and an immutable one, is that an immutable object cannot be altered in any way. There is no difference in the way immutable parameters are passed to functions. Python never passes arguments by value, only by reference.

Only the reference stays local to the function. When one adds to or multiplies a variable, all you're doing is changing the local reference to point at another number. As DaWei says:
Quote:
Originally Posted by DaWei
it isn't a copy, it's a reference to an immutable value, thus, if you reassign it in the function, you're reassigning the reference to another object, not reassigning the value of the original object.
For instance, if you reference the number 10 in one function, and the number 10 in a completely different function in a completely different module, both references point to exactly the same object. You cannot copy or clone an immutable object like a number or a string; there will never be more than one "10" object in memory.
Arevos is offline   Reply With Quote