IsString

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Dec 15 18:12:39 EST 2005


On Wed, 14 Dec 2005 18:17:57 -0800, 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.

No. You can test this yourself:

s = "This is a BIG 'simple' variable." * (10 * 1024**2)
# s will take up at least 320 megabytes of memory

Watch how long it takes to create that big string -- on my (reasonably
fast) PC, there is a noticeable pause of five seconds the first time I
do it, and a longer pause of fifteen seconds the second time, with
noticeable disk-activity.

Pass s to a function, and see if there is a similar pause.

Python NEVER duplicates objects unless you explicitly ask it to, e.g. with
the copy module, or by using slicing s[:].


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

Seems to, but no.

Python does not use a call by value ("arguments are copied when you pass
them to a function") model, nor does it use a call by reference
("pointers to arguments are passed to functions") model, except maybe
internally in the underlying C implementation. If you think about Python
as if it were C or Java, you will forever be confused about its behaviour.
Understand Python's call by object behaviour, and it will all make sense.

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

Terminology is important, because we understand the world through
language. If your language is confused or misleading, your understanding
will also be confused or incomplete.



-- 
Steven.




More information about the Python-list mailing list