Nested function scope problem (- variable definition branch)

H J van Rooyen mail at microcorp.co.za
Mon Jul 31 02:01:19 EDT 2006


"Gerhard Fiedler" <gelists at gmail.com> wrote:

8<---------------------------------

| I'm not sure where you're trying to go. I think that most people (and even
| Bruno, who argued this issue most strongly) call Python variables
| "variables" every now and then, or maybe even usually. But it was helpful
| for me to see the difference between Python variables and, say, C
| variables. I think this has been a useful discussion in this respect. There
| is a difference, and it is important (IMO).
|
| Whether Python variables are in fact "variables" probably depends mostly on
| your definition of "variable", and that's IMO a tough one -- a definition
| of "variable" that includes all those language elements that various
| languages call "variables", and nothing else (that's the tough part).
| Whether that definition exists, and whether it includes Python "variables",
| remains to be seen :)

I am not one for formal definitions but I think something like this:

if s is a python string containing "hello world "
and I can write :

s = s + "some extra stuff"

then for me s is a variable - the fact that in the python implementation there
are two things, namely the original hello world string and the new longer one is
kind of irrelevant - if I try to print s I will get the new longer string, so
from where I stand s is a variable - it has changed over time from having one
"value" to another one...
and that is about the simplest definition you can get - its a symbolic reference
to something that can change over time - and in python it seems to me that every
name is a variable, cos you can tie the name to very different things at
different times:

>>> s = "hello world"
>>> print s
hello world
>>> s = s + " some more stuff"
>>> print s
hello world some more stuff
>>> s = [1,2,3,4,5,6,7,8,9,0]
>>> print s
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> def foo():
 print "banana"

 >>> s = foo
>>> s
<function foo at 0x011DDE30>
>>> s()
banana
>>>

s is surely a variable - there is nothing constant over time about it, and from
this point of view its very "mutable" indeed - and what is more - in every case,
after the assignment, unless you have stored a reference with a different name
to them, the old values look from a programmer's point of view as if they have
been "overwritten" - you can't use anything about s to get at them again...

Now there's a thought - an optional slice notation that slices s over time,
so that s{0} is the current s, and s{-1} the previous one, and so on, with
the default being s[{0}] the {0} being optional....

This should be relatively easy to implement at the point of re binding the name
by replacing the pointer (or whatever) to the object with a stack of them.  I
think you could only do it in python, but I may be wrong...

Another world first for python?  - (TIC)

"Look mommy! - I only use one variable name! "

- Hendrik





More information about the Python-list mailing list