IsString

Xavier Morel xavier.morel at masklinn.net
Tue Dec 13 17:21:02 EST 2005


Steven D'Aprano wrote:
> name = "spam spam spam spam"
> 
> the value of the variable "name" is a pointer, and not a string. Riiight.
> 
Yes, it's a reference to an object of type string holding the value 
<spam spam spam spam>

> def increment(n):
>     """Add one to the argument changing it in place."""
>     # In Pascal, I would need the var keyword to get this behaviour,
>     # but Python is call by reference so all variables are passed 
>     # by reference.
>     n += 1
> 
> x = 1
> increment(x)
> assert x == 2
> 
> but that doesn't work in Python either.
> 

That example is mightily flawed since Python's integers are immutable 
objects.

Here, python creates a new integer object of value "n+1" and binds the 
_local_ name "n" to this new object. n isn't bound to it's initial 
object anymore (the one x is bound to), and therefore can't modify it.

Now use a mutable type instead of an immutable int and you'll notice a 
pass-by-reference behavior.



More information about the Python-list mailing list