[Tutor] list method help
Kent Johnson
kent37 at tds.net
Fri Feb 3 17:21:25 CET 2006
> On 2/3/06, Chris or Leslie Smith <smiles at worksmail.net> wrote:
>>Others could give you a really good answer. I am a BASIC/FORTRAN writer
>>myself, and getting used to the *object* orientation of python took a little
>>while, but after you get the hang of it, it's not bad. In BASIC you think of
>>variables *containing* things, so when you say l=2 and a=l you think of two
>>variables each containing the (what happens to be) the same thing. In
>>python, however, mutable objects (like lists) are *pointed to* by the
>>variable name. so the 'l=range(3)' above tells python to create a list and
>>point the variable name 'l' at it. Then when you say 'a=l' you are telling
>>it to point 'a' at the same thing as 'l'--one object (the list); two
>>references to it.
You definitely have to stop thinking of variables as containers. They
are pointers or references to values. Another way to think of this is
that variables are names for things. You may call me Kent, someone else
might call me Mr. Johnson or Dad, but if I get a haircut, Kent, Mr.
Johnson and Dad all have shorter hair because all three names refer to
the same person.
Python works the same way. Assignment binds a name to a value. So if I say
lst = [0, 1, 2]
I have bound the name 'lst' to a particular list whose value, at the
moment, is [0, 1, 2]. If I then assign
a = lst
this binds the name 'a' to the same value (the list) that 'lst' is bound
to. If I change the bound list, you will see the change whether you
access the list through the name 'a' or the name 'lst'.
Kent
More information about the Tutor
mailing list