Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In python, if you're doing this with non-primitive types, it acts exactly like you want: assigning is done using references. That's why, when you run the following:</p> <pre><code>&gt;&gt;&gt; a = {'key' : 'value'} &gt;&gt;&gt; b = a &gt;&gt;&gt; b['key'] = 'new-value' &gt;&gt;&gt; print a['key'] </code></pre> <p>you get <code>'new-value'</code>. Strictly saying, if you do the following:</p> <pre><code>&gt;&gt;&gt; a = 5 &gt;&gt;&gt; b = a &gt;&gt;&gt; print id(a) == id(b) </code></pre> <p>you'll get True.</p> <p><strong>But!</strong> Because of primitive types are immutable, you cant change the value of variable <code>b</code> itself. You are just able create a new variable with a new value, based on <code>b</code>. For example, if you do the following:</p> <pre><code>&gt;&gt;&gt; print id(b) &gt;&gt;&gt; b = b + 1 &gt;&gt;&gt; print id(b) </code></pre> <p>you'll get two different values.</p> <p>This means that Python created a new variable, computed its value basing on b's value and then gave this new variable the name <code>b</code>. This concerns all of the immutable types. Connecting two previous examples together:</p> <pre><code>&gt;&gt;&gt; a = 5 &gt;&gt;&gt; b = a &gt;&gt;&gt; print id(a)==id(b) True &gt;&gt;&gt; b += 1 &gt;&gt;&gt; print id(b)==id(a) False </code></pre> <p>So, when you assign in Python, you always assign reference. But some types cannot be changed, so when you do some changes, you actually create a new variable with another reference.</p>
 

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