IsString

Mike Meyer mwm at mired.org
Wed Dec 14 22:18:41 EST 2005


"Tuvas" <tuvas21 at gmail.com> writes:
> 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.

No, it doesn't copy them:

>>> def same_object(x, x_id):
...  return id(x) == x_id
... 
>>> a = "abc"
>>> same_object(a, id(a))
True
>>> b = 1
>>> same_object(b, id(b))
True
>>> b = 2 * 20
>>> same_object(b, id(b))
True
>>> 

> However, if you use lists, then it only passes a pointer, or
> tuples as well. Ei, I just ran this through the python IDE.

It passes a reference to the object in both cases.

>>>> 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.

Except that modstring didn't do anything to change the value of x. It
bound the name var in the modstring function to the string "Blah".

> 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 don't. In a conventional language - including assembler - a
variable is a *compile-time* object that refers to a specific bit of
memory. Mentioning the name of the variable in your source causes code
to be emitted that loads the value stored in that memory. An
assignment statement causes code to be emitted to that stores a value
in that memory. By the time you get to run time, the variable per se
no longer exists - all references to it have been converted to
references to the memory it specifies.  This is grossly
oversimplified, and not true for all languages, including some that
would otherwise be considered conventional.

> Other than this, I basically see a fight on terminology, and that's
> that.

Could well be.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list