[Tutor] Re: informaton about some specific variable locaton ... does computer needs two informations ???

Alan Gauld alan.gauld at freenet.co.uk
Wed Nov 19 04:45:21 EST 2003


----- Original Message ----- 
From: "Tadey" <tayiper at volja.net>
To: "Alan Gauld" <alan.gauld at freenet.co.uk>
Sent: Wednesday, November 19, 2003 7:53 AM
Subject: informaton about some specific variable locaton ... does
computer needs two informations ???


> I have read some book about programming languages in general, and in
chapter
> about variables, it says something like (I had to translate it from
> Slovenian - as accurate as I could)
>
> > START <
>
> Access to certain location in memory is possible by addressing this
location
> ...
> Adress could be the name of "variable" (Note: with variable, here
meaning
> just a number which could change - more like in math, not
programming term
> variable) or "pointer" ("index", "indicator"). In programming
languages the
> name of "variable" (again math term) could means value, which is
stored on
> this location, from which we read this value, or it means the adress
of
> location, where we want to store (assign) the new value.    ???
equivocality
> ???
>
> (Note -> the important part) The name of "pointer" also could
represent
> value or adress, but not the (Note: probably adress of ...)
location, where
> this pointer  indicates to, but but location, where the value of
pointer is
> stored.    ??? equivocality again ???
>
> > END OF TEXT <
>
>
> So, as I understand this tex wants to say, that when for example
assigning
> number (value) 4 to valiable a (a = 4), there are actually two
informations,
> which are important, which computer stores ...
>
> one is:
> - that on location a, value 4 is stored ...
>
> and second:
> - that this specific physical location in memory is "called",
"assigned" as
> variable a (by us) ...


Absolutely correct.
However how variables are implemented varies a lot between programming
languages. Python, internally, uses a dictionary for variables. So a
variable
name is simply a lookup key to a dictionary and the value is whatever
is held
against that key.

You can think of an assignment

a = 42

As being almost the same as

Variables['a'] = 42

Where Variables is the Python variable dictionary.

Its not quite as simple as that but not much more complicated either.

However languages like C actually allocate a bit of computer memory
to a variable so that

int a = 42;

actually allocates enough memory to hold an integer and binds the
name 'a' to its address. Then the assinment actually sets that menory
location to hold 42.

> ... which is something, computer should also remember to recognize
some
> location as "named" as a ("the value of pointer"), beside knowing
the value
> of that location.

This hopefully explains why C has a notion of pointers as well as
variables,
because a variable can be defined to hold a memory address. That
address
can be the address of another variable.

int* b = a

Allocates enough memory to hold an address and binds the name 'b' to
that address, it then fills the address with the address of 'a'.

Python doesn't need to do that since variables are dictionary keys so
that
all variables are in effect pointers to the real data (which can be of
any type).

> Please, tell me, if I am right, ... some variables
> like, a, b, c, d, etc. are fixed, default - meaning that a or b or c
always
> points to only one, and only one certain physical location in
memory)  ?!?

In languages like C that's true, in Python (and some other languages)
it is
slightly different. To be honest trying to think of variables at that
level of detail
as a beginner is probably just confusing. In my tutorial I use the
example
of a postal sorting office. The data is what gets put in the boxes and
the
variables are the labels stuck on the front of the boxes. Data in a
box
without a label gets thrown in the garbage. That's a pretty accurate
illustration of how Python does things.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list