Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'digest': 0.04; 'string.': 0.05; 'differently': 0.07; 'extent': 0.07; 'modify': 0.07; 'you?re': 0.07; 'string': 0.09; '204);': 0.09; '204,': 0.09; '51,': 0.09; 'answering': 0.09; 'block;': 0.09; 'explanation': 0.09; 'function,': 0.09; 'variable,': 0.09; 'you?ve': 0.09; '"it\'s': 0.16; '0.5em': 0.16; '0.5em;': 0.16; '255);"': 0.16; '51);': 0.16; 'behave': 0.16; 'differs': 0.16; 'edition.': 0.16; 'pre-wrap;': 0.16; 'pre;': 0.16; 'rgb(204,': 0.16; 'rgb(51,': 0.16; 'tuples,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'finished': 0.19; 'value.': 0.19; 'memory': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; ' User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: dave em , python-list@python.org Subject: Re: Explanation of list reference References: <13208de8-0f85-4e60-b059-dc087c8fda41@googlegroups.com> In-Reply-To: <13208de8-0f85-4e60-b059-dc087c8fda41@googlegroups.com> Content-Type: multipart/alternative; boundary="------------070409070009020703050302" X-Mailman-Approved-At: Fri, 14 Feb 2014 20:22:11 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 136 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392405732 news.xs4all.nl 2931 [2001:888:2000:d::a6]:58222 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66316 This is a multi-part message in MIME format. --------------070409070009020703050302 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 02/14/2014 12:08 PM, dave em wrote: > Hello, > > Background: My twelve y/o son and I are still working our way through Invent Your Own Computer Games with Python, 2nd Edition. > (We finished the Khan Academy Javascript Tutorials is the extent of our experience) > > He is asking a question I am having trouble answering which is how a variable containing a value differs from a variable containing a list or more specifically a list reference. > > I tried the to explain as best I can remember is that a variable is assigned to a specific memory location with a value inside of it. Therefore, the variable is kind of self contained and if you change the variable, you change the value in that specific memory location. > > However, when a variable contains a list reference, the memory location of the variable points to a separate memory location that stores the list. It is also possible to have multiple variable that point to the memory location of the list reference. And all of those variable can act upon the list reference. > > Question: Is my explanation correct? If not please set me straight :) > > And does anyone have an easier to digest explanation? > > Thanks in advance, > Dave You've got it backwards. In Python, /everything/ is a reference. The variable is just a "pointer" to the actual value. When you change a variable, you're just changing the memory location it points to. Strings, ints, tuples, and floats behave differently because they're /immutable/. That means that they CANNOT modify themselves. That's why all of the string methods return a new string. It also means that, when you pass one two a function, a /copy/ of it is made and passed instead. So, back to the original subject. Everything is a reference. When you do this: |x = [1,2,3] x = [4,5,6] | x now points to a different memory location. And, when you do this: |x[0] =99000 x[0] =100 | you're just changing the memory location that |x[0]| points to. -- --Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated." --------------070409070009020703050302 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 02/14/2014 12:08 PM, dave em wrote:
Hello,

Background:  My twelve y/o son and I are still working our way through Invent Your Own Computer Games with Python, 2nd Edition.
(We finished the Khan Academy Javascript Tutorials is the extent of our experience)

He is asking a question I am having trouble answering which is how a variable containing a value differs from a variable containing a list or more specifically a list reference.

I tried the to explain as best I can remember is that a variable is assigned to a specific memory location with a value inside of it.  Therefore, the variable is kind of self contained and if you change the variable, you change the value in that specific memory location.

However, when a variable contains a list reference, the memory location of the variable points to a separate memory location that stores the list.  It is also possible to have multiple variable that point to the memory location of the list reference.  And all of those variable can act upon the list reference.

Question:  Is my explanation correct?  If not please set me straight :)

And does anyone have an easier to digest explanation?

Thanks in advance,
Dave

You’ve got it backwards. In Python, everything is a reference. The variable is just a “pointer” to the actual value. When you change a variable, you’re just changing the memory location it points to.

Strings, ints, tuples, and floats behave differently because they’re immutable. That means that they CANNOT modify themselves. That’s why all of the string methods return a new string. It also means that, when you pass one two a function, a copy of it is made and passed instead.

So, back to the original subject. Everything is a reference. When you do this:

x = [1,2,3]
x = [4,5,6]

x now points to a different memory location. And, when you do this:

x[0] = 99000
x[0] = 100

you’re just changing the memory location that x[0] points to.



-- 
--Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
--------------070409070009020703050302--