Scope troubles with a swap function
Hans Nowak
hnowak at cuci.nl
Fri Aug 10 11:00:26 EDT 2001
>===== Original Message From "Matthew D. Wood" <woodm at equire.com> =====
>Ok, I've been banging my head against this for a sufficiently long time
>that I feel justified in presenting this personal challenge to the list.
>
>How do you make a swap function?
You don't need to. :) Use:
a, b = b, a
>How do you make a function or a callable object instance that will do
>what the following code intends to do:
>
> def swap (heaven, hell) :
> purgatory = heaven
> heaven = hell
> hell = purgatory
Python's internal workings are very different from languages like C and
Pascal. A Python "variable" is not a location in memory that you can fill with
arbitrary data. This Pascal code:
X := 3;
X := 42;
changes the value of X, which resides at a certain location in memory. But the
equivalent Python code
x = 3
x = 42
does not do exactly the same; rather, it binds the name 'x' to the value 3,
then rebinds that name to the value 42. Other people explain this better than
me; see
http://w1.132.telia.com/~u13212494/guides/python-objects.htm
HTH,
--Hans Nowak
More information about the Python-list
mailing list