[Tutor] list method help

Rinzwind w.damen at gmail.com
Fri Feb 3 16:58:56 CET 2006


On 2/3/06, Chris or Leslie Smith <smiles at worksmail.net> wrote:
> Rinzwind wrote:
> | Well Chris or Leslie Smith.
> |
> | This bit l[:]=l[-1:]+l[0:-1] I think is VERY elegant. When I saw this
> | in your post I tought: DUH.
> | I did the same with 1 line more but I am still new to python ;)
> |
> You're seeing the beauty of the language at work :-)

I sure do :)
I also love dictionaries, tuples, and lists. Thoroughbred Basic
doesn't work like that :*

> | Regarding the rest of the 'aside'.
> |
> | What is the reasoning behind this:
> |
> | ###
> |||| l=range(3)
> |||| a=l     # 'a' is pointing at what 'l' is pointing at
> |||| l[0]=42 # I make a change to the thing that 'l' points to
> |||| print a
> | [42, 1, 2]
> |||| print l
> | [42, 1, 2]
> ||||
> | ###
> |
> | -Why- are these 2 bound together? Is there a specific reasoning
> | behind it?
> |
> | Cuz in Basic
>
> 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

10 FOR P1= 1 TO 10000; PRINT 'Hello Chris'; NEXT P1

That Basic not VB ;-)

> 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.

Yes, I read something about it concerning ID's assigned to variables.
So A = [1,2,3,4] has an ID 209370
And L =  A gives L the ID 209370 matching them together.

It's cool :-) and would save me alot of  A=L stmts in Basic.
'-)

<..>
> | What is the reasoning behind this:
> |
> | ###
> |||| l=range(3)
> |||| a=l     # 'a' is pointing at what 'l' is pointing at
> |||| l[0]=42 # I make a change to the thing that 'l' points to
> |||| a=l  <------- this one added by ME.
> |||| print a
> | [42, 1, 2]
> |||| print l
> | [42, 1, 2]
> ||||
> | ###
>
> I know what you are getting at, but from what I wrote above do you see the
> difference yet? In basic, saying 'a=l' has created a new variable/value item
> and if you want the new change to be reflected in a after making a change to
> l you have to 'copy it there' with another a=l statement. In python there is
> only one object (the [0, 1, 2]) with two variables pointing to it (a and l).
> Those pointer-varaibles are like aliases for the same thing.

Oh I understand it :) Was just wondering -why-. I sometimes am like a
10 year old wanting not to know how it works by why it was created to
work that way. There must be a reason ;-)

<..>
>
> p.s. I am Chris. My wife and I share the same account, so that's why there
> is a "or" in the email name :-)
>
>

Well I can't guess if it's you, your wife or you are both behind your
PC beating eachother over the head to decide who can answer my
questions :-D


More information about the Tutor mailing list