Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>So in a function the adding a 6 to the set doesn't show but it does when not in a function?</p> </blockquote> <p>No, that is not what happens.</p> <p>What happens is that, when you execute <code>mylist = mylist + [6]</code>, you are effectively creating an entirely new list and putting it in the local <code>mylist</code> variable. This <code>mylist</code> variable will vanish after the execution of the function and the newly created list will vanish as well.</p> <p>OTOH when you execute <code>mylist.append(6)</code> you do not create a new list. You get the list already in the <code>mylist</code> variable and add a new element to this same list. The result is that the list (which is pointed by <code>list2</code> too) will be altered itself. The <code>mylist</code> variable will vanish again, but in tis case you altered the original list.</p> <p>Let us see if a more visual explanation can help you :)</p> <h1>What happens when you call <code>proc()</code></h1> <p>When you write <code>list1 = [1, 2, 3, 4, 5]</code> you are creating a new list object (at the right side of the equals sign) and creating a new variable, <code>list1</code>, which will point to this object.</p> <p><img src="https://i.imgur.com/7Wifq.png" alt="Creating new list instance and global variable"></p> <p>Then, when you call <code>proc()</code>, you create another new variable, <code>mylist</code>, and since you pass <code>list1</code> as parameter, <code>mylist</code> will point to the same object:</p> <p><img src="https://i.imgur.com/KowHP.png?1" alt="Calling method creates local variable"></p> <p>However, the operation <code>mylist + [6]</code> <em>creates a whole new list object</em> whose contents are the contents of the object pointed by <code>mylist</code> plus the content of the following list object - that is, <code>[6]</code>. Since you attribute this new object to <code>mylist</code>, our scenario changes a bit and <code>mylist</code> does not point to the same object pointed by <code>list1</code> anymore:</p> <p><img src="https://i.imgur.com/X6yaB.png?1" alt="mylist points to new list object"></p> <p>What I have not said is that <code>mylist</code> is a <em>local variable</em>: it will disappear after the end of the <code>proc()</code> function. So, when the <code>proc()</code> execution ended, the <code>mylist</code> is gone:</p> <p><img src="https://i.imgur.com/8NP33.png?1" alt="mylist is gone"></p> <p>Since no other variable points to the object generated by <code>mylist + [6]</code>, it will disappear, too (since the garbage collector* will collect it):</p> <p><img src="https://i.imgur.com/ddifs.png?1" alt="GC collects the list"></p> <p>Note that, in the end, the object pointed by <code>list1</code> is not changed.</p> <h1>What happens when you call <code>proc2()</code></h1> <p>Everything changes when you call <code>proc2()</code>. At first, it is the same thing: you create a list...</p> <p><img src="https://i.imgur.com/7Wifq.png" alt="Creating new list instance and global variable"></p> <p>...and pass it as a parameter to a function, which will generate a local variable:</p> <p><img src="https://i.imgur.com/KowHP.png?1" alt="Calling method creates local variable"></p> <p>However, instead of using the <code>+</code> concatenation operator, which generates a new list, you apply the <code>append()</code> method to the existing list. The <code>append()</code> method <em>does not create a new object</em>; instead, it _changes the existing one:</p> <p><img src="https://i.imgur.com/rv7Br.png?1" alt="Appending a value to a list"></p> <p>After the end of the function, the local variable will disappear, but the original object pointed by it and by <code>list1</code> will be already altered:</p> <p><img src="https://i.imgur.com/VDnSF.png?1" alt="It is still altered"></p> <p>Since it is still pointed by <code>list1</code>, the original list is not destroyed.</p> <p><strong>EDIT</strong>: if you want to take a look at all this stuff happening <strong>before your eyes</strong> just go to <a href="http://www.pythontutor.com/visualize.html#code=list1+=+%5B1,2,3,4%5D%0Alist2+=+%5B1,2,3,4%5D%0A%0Adef+proc%28mylist%29:%0A++++mylist+=+mylist+++%5B6%5D%0A%0Adef+proc2%28mylist%29:%0A++++mylist.append%286%29%0A%0A#+Can+you+explain+the+results+given+by+the+four+print+statements+below?%2bRemove%0A#%2bthe%2bhashes%2b#%2band%2brun%2bthe%2bcode%2bto%2bcheck.%0A%0Aprint%2b%28list1%29%0Aproc%28list1%29%0Aprint%2b%28list1%29%0A%0Aprint%2b%28list2%29%0Aproc2%28list2%29%0Aprint%2b%28list2%29&amp;mode=display&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=16" rel="nofollow noreferrer">this radically amazing simulator</a>:</p> <p><img src="https://i.stack.imgur.com/4p5dC.gif" alt="enter image description here"></p> <p>* If you do not know what is garbage collector... well, you will discover soon after understanding your own question.</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.
 

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