<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 02/14/2014 12:08 PM, dave em wrote:<br>
</div>
<blockquote
cite="mid:13208de8-0f85-4e60-b059-dc087c8fda41@googlegroups.com"
type="cite">
<pre wrap="">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
</pre>
</blockquote>
<small><font face="Arial">
<div style=""
data-md-original="You've%20got%20it%20backwards.%20In%20Python%2C%20*everything*%20is%20a%20reference.%20The%20variable%20is%20just%20a%20%22pointer%22%20to%20the%20actual%20value.%20When%20you%20change%20a%20variable%2C%20you're%20just%20changing%20the%20memory%20location%20it%20points%20to.%3Cbr%3E%3Cbr%3EStrings%2C%20ints%2C%20tuples%2C%20and%20floats%20behave%20differently%20because%20they're%20*immutable*.%20That%20means%20that%20they%20CANNOT%20modify%20themselves.%20That's%20why%20all%20of%20the%20string%20methods%20return%20a%20new%20string.%20It%20also%20means%20that%2C%20when%20you%20pass%20one%20two%20a%20function%2C%20a%20*copy*%20of%20it%20is%20made%20and%20passed%20instead.%3Cbr%3E%3Cbr%3ESo%2C%20back%20to%20the%20original%20subject.%20Everything%20is%20a%20reference.%20When%20you%20do%20this%3A%3Cbr%3E%3Cbr%3E%60%60%60python%3Cbr%3Ex%20%3D%20%5B1%2C2%2C3%5D%3Cbr%3Ex%20%3D%20%5B4%2C5%2C6%5D%3Cbr%3E%60%60%60%3Cbr%3E%3Cbr%3Ex%20now%20points%20to%20a%20different%20memory%20
location.%20And%2C%20when%20you%20do%20this%3A%3Cbr%3E%3Cbr%3E%60%60%60python%3Cbr%3Ex%5B0%5D%20%3D%2099000%3Cbr%3Ex%5B0%5D%20%3D%20100%3Cbr%3E%60%60%60%3Cbr%3E%3Cbr%3Eyou're%20just%20changing%20the%20memory%20location%20that%20%60x%5B0%5D%60%20points%20to."
class="markdown-here-wrapper" data-md-url="Thunderbird"
id="markdown-here-wrapper-676830">
<p style="margin: 1.2em 0px ! important;">You’ve got it
backwards. In Python, <em>everything</em> 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.</p>
<p style="margin: 1.2em 0px ! important;">Strings, ints,
tuples, and floats behave differently because they’re <em>immutable</em>.
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 <em>copy</em> of
it is made and passed instead.</p>
<p style="margin: 1.2em 0px ! important;">So, back to the
original subject. Everything is a reference. When you do
this:</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px 3px 3px 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px 3px 3px 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;display: block; padding: 0.5em; color: rgb(51, 51, 51); background: none repeat scroll 0% 0% rgb(248, 248, 255);" class="language-python">x = [<span style="color: rgb(0, 153, 153);" class="number">1</span>,<span style="color: rgb(0, 153, 153);" class="number">2</span>,<span style="color: rgb(0, 153, 153);" class="number">3</span>]
x = [<span style="color: rgb(0, 153, 153);" class="number">4</span>,<span style="color: rgb(0, 153, 153);" class="number">5</span>,<span style="color: rgb(0, 153, 153);" class="number">6</span>]
</code></pre>
<p style="margin: 1.2em 0px ! important;">x now points to a
different memory location. And, when you do this:</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px 3px 3px 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px 3px 3px 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;display: block; padding: 0.5em; color: rgb(51, 51, 51); background: none repeat scroll 0% 0% rgb(248, 248, 255);" class="language-python">x[<span style="color: rgb(0, 153, 153);" class="number">0</span>] = <span style="color: rgb(0, 153, 153);" class="number">99000</span>
x[<span style="color: rgb(0, 153, 153);" class="number">0</span>] = <span style="color: rgb(0, 153, 153);" class="number">100</span>
</code></pre>
<p style="margin: 1.2em 0px ! important;">you’re just changing
the memory location that <code style="font-size: 0.85em;
font-family:
Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em;
padding: 0px 0.3em; white-space: pre-wrap; border: 1px
solid rgb(234, 234, 234); background-color: rgb(248, 248,
248); border-radius: 3px 3px 3px 3px; display: inline;">x[0]</code>
points to.</p>
</div>
<br>
</font></small><br>
<pre class="moz-signature markdown-here-signature" cols="72">--
--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."
</pre>
</body>
</html>