Variables

Richard Blackwood richardblackwood at cloudthunder.com
Mon Apr 25 01:18:52 EDT 2005


I thought I'd share a piece of the discussion a friend of mine is having 
with a mathematician/programmer on this issue of variables:

> int foo = 5;
>
> Is foo not a variable? Within the scope of that statement, foo can be 
> no other value but 5. Is foo not a constant? Are constants and 
> variables not mutually exclusive? But wait, what if I write:
>
Yes, foo, here, is a variable in the program.  In this case, it is a 
variable that has been given a value at the same time that it is 
declared. It can, within the program, be changed at any time, as you 
illustrate with the following example:
Yet what if my program is a mere two lines where foo does not change 
(the following code is in Python):

foo = 5
print foo

Does the fact that foo fails to change in the execution of the /entire/ 
program invalidate its status as a variable? Or is it merely the 
potential to change (as enforced by the respective compiler) that 
defines foo as a variable? In a program where my name-value-couple (foo) 
does not change values, am I to consider foo a constant? What are the 
consequences of doing otherwise?

In programming (i.e. versus math) is the concept of variable and 
constant dependent on how a compiler processes identifiers and values in 
a program, and the various restrictions imposed on programmers regarding 
how they can manipulate those identifiers and values? For example, in 
C/C++ one can actually define constants, but this has a strict meaning 
in the sense that the compiler forces you to give constants known (?) 
values within the code which are then immutable. if I write the 
following program (pseudo-code):

const X = 5
print X
Y = 5
print Y

The only thing that makes X any more of a constant than Y is the fact 
that X is treated by the compiler as a constant--- I cannot change X's 
value with additional code, but I can change Y's, I merely happened to 
chose not to. From this standpoint, the notion of variable and constant 
in programming is very much bound to the way the language treats each, 
the "can"s and "cannot"s imposed on the programmer by the respective 
language. In Python, the interpreter unambiguously knows nothing about 
constants, and as many would argue, nothing about variables either (only 
names and values/data/objects).

>
> ...
>
>> ...
>
>> Is the definition of variable actually different between math and 
>> programming? And regardless, is the notion of variable so very 
>> ambiguous, even within each respective field?
>
>
> Variable is the same in both contexts.  In a computer *program*, 
> however, you can have statements such as "x = x+1" which is not 
> appropriate in mathematics.  You need there to be understanding that 
> within a computer a variable is treated as a place in the memory.  The 
> statement "x=x+1" refers to the process of looking at what is in x, 
> adding 1 to that value, and substituting that value back into the 
> location in memory. ... 

Many would argue a more general definition of variable: variables are 
symbolic references to data/values which can change. One math text in my 
possession concurs with this programming definition by stating a 
variable to be a symbol replaceable by a value which can change. Another 
(Scott Foresman, UCSMP, 1993) writes that a variable is "a letter or 
other symbol that can be replaced by any numbers (or other objects)." Is 
the definition of a variable, particularly in math, so simple? Your 
definition (identical to Raphael A. Finkel's [1996]) implies that, in 
such languages such as Python where names (i.e. foo) are mapped to 
values, there are no variables. For example, when I write foo = 5 in 
Python, the interpreter will create an integer 5 in memory, a name foo 
(unless it already exists), and then bound the name foo to the value 
int(5). Foo is not a place in memory in this sense, but instead a 
reference, a pointer that can direct us to values/data stored in various 
memory locations. Where foo points to (and hence what value foo provides 
us) can change. This is not like in C where foo would be a place in 
memory. In C, if I write int foo = 5, a block of memory is allocated for 
values of foo and 5 is stored there. If I later write foo = 6, the 
contents of foo's memory block are overwritten with the integer 6. In 
this memory based sense, values in C are bound to names, whereas in 
Python names are bound to values. Is it only the former case which 
defines a variable? Many would argue yes. Foo in Python is merely a name 
which can be mapped to a value they would say. On the other hand, many 
would argue no. They would say that a variable is simply an abstract or 
concrete "place" to store data. In the case of Python and the foo 
example, they would argue that foo is a variable in an abstract sense, 
though indeed not so semantically. But what functional good does focus 
on such details provide?

I agree with both camps. However, I posit that perhaps foo *is* 
semantically a variable in the practical sense that it exists in memory 
as a store of a value--- not 5 as in foo = 5, but the *reference* to the 
so-called anonymous object (the integer 5 in this case). The "value" of 
foo in Python is its reference to whatever value it is destine to return 
to us (as if that value was it's own).

Would you agree that the concept of variables differs in very important 
ways between the domains of math and programming?



More information about the Python-list mailing list