IsString

bonono at gmail.com bonono at gmail.com
Wed Dec 14 22:14:08 EST 2005


Tuvas wrote:
> I don't know if I can help with this much, I'm still somewhat new to
> python, but it is my understanding that "simple" variable, ei, strings,
> ints, etc, although they don't have such names, behave like variables,
> ei, if you pass them to a function, the function will copy them into a
> new spot. However, if you use lists, then it only passes a pointer, or
> tuples as well. Ei, I just ran this through the python IDE.
>
> >>> x="Test"
> >>> def modstring(var):
> 	var="Blah"
> >>> modstring(x)
> >>> print x
> Test
>
> This seems to indicate that the variable is copied, as the value didn't
> change. Weither or not Python keeps the variables as pointers
> internally, it doesn't really matter. Actually, all languages do such
> things, except assembly.
No. They are not copied, at least not at function invokation. The
parameter in the functions scope got rebound(that is the term python
people like to use) to a new value(or object in python term as
everything in python is an object) if you use the "=" operator.

x="test"
y=["test"]
def mod_1(v):
  v="blah"

def mod_2(v):
  v[0]="blah"

mod_1(x)
print x
mod_1(y)
print y
mod_2(x)
print x
mod_2(y)
print y

You can see that neither x or y changed with mod_1, regardless whether
it is mutable or not. mod_2(x) is an error as it kind of use 'v' as an
object pointer and invoke some method on it(the object) which doesn't
exist for x. mod_2(y) works as expected, because y in this case is sort
of an object which is a container which has the "[]" method.

> I just ran the test on lists and tuples, it was the same results,
> nothing moved.
>
> Other than this, I basically see a fight on terminology, and that's
> that.




More information about the Python-list mailing list