Variables

Mike Meyer mwm at mired.org
Sun Apr 24 00:03:47 EDT 2005


Richard Blackwood <richardblackwood at cloudthunder.com> writes:

> Robert Kern wrote:
>> Richard Blackwood wrote:
>>> To All:
>>>
>>>    Folks, I need your help. I have a friend who claims that if I write:
>>>
>>> foo = 5
>>>
>>> then foo is NOT a variable, necessarily. If you guys can define for
>>> me what a variable is and what qualifications you have to back you,
>>> I can pass this along to, hopefully, convince him that foo is
>>> indeed a variable.
>>
>> None of us can do that unless you tell us what he thinks the word
>> "variable" means. The terminology is a bit fluid. I suspect that
>> your friend applying a somewhat restricted notion of "variable" that
>> coincides with the behavior of variables in some other language.
>>
> Indeed, this language is math. My friend says that foo is a constant
> and necessarily not a variable. If I had written foo = raw_input(), he
> would say that foo is a variable. Which is perfectly fine except that
> he insists that since programming came from math, the concept of
> variable is necessarily the identical. This can not be true. For
> example, I may define foo as being a dictionary, but I can not do this
> within math because there is no concept of dictionaries within
> mathematics; yet foo is a variable, a name bound to a value which can
> change.

Wrong on two counts.

First, new disciplines often redefine words to mean something
different than the disciplines they were derived from. Variable is a
good example of that. In math, a variable is a placeholder in an (a
system of) equation(s), and will have associated with it a (possibly
empty) set of values that satisfy the equation(s). In programming, a
variable holds a value of some kind. As you point out, the value may
be complicated, in that it holds one or more objects.

In math, if you say "foo = 5", foo is a variable, with a single value
(5) that satisfies the equation. In Python, if you say "foo = 5", foo
is a variable, that currently references the value 5.

What Python calls a "dictionary" math calls a "mapping", or sometimes
a "function" (though that usage is usually reserved for mappings
to/from numbers of some kind). A mapping is a relationship between
elements of two sets, pairing elements of the first set with elements
of the second set. In a Python dictionary, the first set is keys(), and the
second set is values(). In both cases, the pairing is unidirectional,
as picking an element from the first set (keys) selects a unique
element from the second set (values), but picking an element from the
second set (values) may pick more than one element from the first set
(keys). See <URL: http://www.answers.com/topic/function > for more
information.

Note that lists and tuples are both mappings, though in their case the
first set is restricted to the integers. If you check the Python
documentation, you'll see these three types (dictionaries, lists and
tuples) documented in a section called "Mapping Types".

        <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