Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, let me try to explain with some images.</p> <p>In Python, everything is an object. Those objects are <em>referenced</em> by variables. Some kinds of objects, such as lists and tuples, just store references to other objects.</p> <p>That said, when you execute</p> <pre><code>myVar = ["jhhj", "hgc"] myTuple = ([1,2,3], [4,5,6], myVar) </code></pre> <p>You get more or less this scenario:</p> <p><img src="https://i.imgur.com/X5ZpQ.png" alt="A tuple pointing to three lists; a list pointing to two strings"></p> <p>Each object is represented by a box/rectangle. We have two string objects, <code>"jhhj"</code> and <code>"hgc"</code>. Also, we have a list object, pointed by the variable <code>myVar</code>; this list object points to both string objects. Also, we have a tuple object, referenced by <code>myTuple</code>; this tuple object points two other lists and the list referenced by <code>myVar</code>.</p> <p>When you execute</p> <pre><code>myVar.append('lololol') </code></pre> <p>what does happen? Well, the list object (that is incidentally pointed by <code>myVar</code>) starts to referenced one more value, the string object <code>"lololol"</code>:</p> <p><img src="https://i.imgur.com/5KrCJ.png" alt="A tuple pointing to three lists; a list pointing to two strings; a new string is added to the list"></p> <p>Note that <code>myVar</code> still references the list object. What happened is that the list object <em>changed</em>. You can look at this list object both from <code>myVar</code> or from the tuple, you will see the same object with the same change.</p> <p>OTOH, when you execute </p> <pre><code>myVar = "lol" myTuple = ([1,2,3], [4,5,6], myVar) </code></pre> <p>You get something like this:</p> <p><img src="https://i.imgur.com/oCJij.png" alt="A tuple pointing to two lists and a string, also pointed by a variable"></p> <p>Now <code>myVar</code> points to the string object <code>"lol"</code>, as well the tuple references it at its third position. Now, if you execute</p> <pre><code>myVar = "lolol" </code></pre> <p>you are just making <code>myVar</code> to point to another object. The tuple object still points to <code>"lol"</code> as before:</p> <p><img src="https://i.imgur.com/HcbyJ.png" alt="A tuple pointing to two lists and a string, also pointed by a variable. The variable now references another string."></p> <p>So, if you attribute a new value to a variable, it will just change the value pointed by this variable. The previous value referenced by the variable will still exist* and any other variable or object pointing to it will stay pointing to it. Just the attributed variable will change.</p> <p>PS: Also, <a href="https://stackoverflow.com/questions/10748158/why-does-the-operator-not-change-a-list-while-append-does/10748191#10748191">I answered a vaguely related question some time ago</a>. You may find the answer helpful.</p> <p>* Except if it is collected by the garbage collector, but this is another long history.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload