<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="367453" data-post-id="367453">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="al2o3cr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/120/3457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  al2o3cr
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s the simplest demonstration of the difference between “mutating” and “rebinding” I can think of:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = 1
captured_fun = fn -&gt; x end
x = 2
captured_fun.()
</code></pre>
<p>This results in <code>1</code>, because the <code>x</code> in <code>captured_fun</code> refers to the value bound to <code>x</code> when it was defined - not when it was called.</p>
<p>Compare the result of an identical sequence in a language with mutable variables, for instance Javascript:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = 1;
captured_fun = function () { return x; }
x = 2;
captured_fun()
</code></pre>
<p>Here the <code>x</code> in <code>captured_fun</code> refers to the mutable variable <code>x</code>, so it results in <code>2</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367453" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-367453" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367453"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="367454" data-post-id="367454">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The important thing is that nothing else can have a reference to that memory.  Rebinding does make it confusing but it’s just a convenience in a very tight scope.  If you rebind once, you can still access the old value via the pin (<code>^</code>) operator.  And by tight scope I mean:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = 1
if true, do: x = 2
x # x is still `1` here.
</code></pre>
<p>So rebinding may not fit an extremely strict definition of immutability, but you’re getting the most important part which is that data can’t change from under you.</p>
<hr>
<p>Ops, didn’t see <a class="mention" href="/u/al2o3cr" rel="nofollow">@al2o3cr</a> responding but I went and wrote this so I’m gonna post, heh.</p>
<p>PS, welcome to the forum!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367454" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-367454" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367454"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="367462" data-post-id="367462">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lud" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/120/14382_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lud
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes you are confusing yourself more I guess <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> because rebinding and immutability are not actually related.</p>
<p>Immutability is the fact that you cannot change <em>values</em> in memory. If you pass some value to a function (for instance by passing a variable), you have the guarantee that the function cannot change whatever you hold in that variable. And on the other hand the author of that function has the guarantee that external code cannot change what is passed down.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">n = 123
do_stuff(n, some_fun)
# Here `n` cannot be something else than `123`

def do_stuff(arg, fun) do
  fun.(arg)
  # Here `arg` cannot be something else than `123`
end
</code></pre>
<p>Rebinding or not has nothing to do with that, it’s just syntax. Edit: But the syntax is convenient, as said above. Erlang chose to have the <code>&lt;var&gt; =</code> expression match on already-bound variables, whereas Elixir chose to implement rebinding. Another language could make that a syntax error, like <code>const</code> in JavaScript (though those are not actual “constants” since you can mutate object properties for instance). But AFAIK no language (at the parser/AST/bytecode-compiler level) on the BEAM will make that expression mutate the values because the underlying VM offer no means to do so (though I’m now talking about something I know too little about).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367462" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-367462" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367462"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="367464" data-post-id="367464">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It’s also helpful to see example with complex data structures:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># python
a = {'foo': 'bar'}
b = a
b = {'foo': 'BAZ'}
a['foo'] # This is 'BAZ'
</code></pre>
<p>In Elixir, this is impossible.  As <a class="mention" href="/u/lud" rel="nofollow">@lud</a> alluded to, there isn’t even any syntax you can use to enable this.  If you do want mutability you have to get into things like ETS (built-in key-value store).</p>
<p>EDIT: Figured I may as well show the Elixir example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">a = %{foo: "bar"}
b = a
b = Map.put(b, :foo, "BAZ")
a.foo # This is still "bar"
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367464" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-367464" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367464"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #25"></div>
  </section>
</div>
    <div class="postbit" id="367465" data-post-id="367465">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="New2Elixir" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  New2Elixir
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you both, that was quick..</p>
<p>LOL, I’m so new I used Grok to explain al203cr’s snippet.<br>
I’m probably way off, but here goes.</p>
<p>So, captured_fun is an anonymous function that simply returns x, currently is 1.<br>
Next statement rebinds x to 2.<br>
Calling captured_fun prints 1 because when it was created it x was 1.<br>
So it sounds as though the function is not actually using the variable as a variable is non-FP would expect, but more like when the function was created/instanced(?), the value of x was copied into the function.<br>
If after the last statement you were to simply print x though, it would print 2, right?</p>
<p>If true, then this seems like when you define a function is pretty important.</p>
<p>What if my program were actually naively written similar to that, and I needed x to return 2?<br>
Do I need to have to think about when I am defining functions, or is this my imperative experience mismatching with FP?</p>
<p>I read somewhere IIRC that this is done in part for safety/data corruption as with numerous processes this reduces the chance for one process to change a variable while another is working on it.<br>
That makes sense if true, however if a variable is changing it would seem difficult to know whether or not your function will be accessing the actual current value.</p>
<p>Appreciate anyone willing to waste their time enlightening me.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367465" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-367465" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367465"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #26"></div>
  </section>
</div>
    <div class="postbit" id="367474" data-post-id="367474">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mudasobwa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudasobwa/120/5298_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mudasobwa
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Cure</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well, in this discussion we somehow swept under a rug the fact that <code>=</code> is <em>not</em> an assignment. It’s rather a <em>match</em>.</p>
<p>You might do the following:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = 1
1 = x
</code></pre>
<p>And the code above is perfectly legit.</p>
<p>Once it is a <em>match</em>, all <em>variables</em> on the LHO are <em>assigned</em> (or, if you wish, reassigned.) That is a design decision, unlike erlang, and José explained it in details <a href="https://dashbit.co/blog/comparing-elixir-and-erlang-variables" rel="noopener nofollow ugc">here</a>. Actually, one might do <code>%{foo: x} = %{foo: 42}</code> and this is <em>exactly the same construct</em> as in <code>x = 1</code>.</p>
<p>If one wants to prevent rebinding on the LHO, they do <em>pin</em> variables with <a href="https://hexdocs.pm/elixir/Kernel.SpecialForms.html#%5E/1" rel="noopener nofollow ugc"><code>^</code></a>.</p>
<p>I hope that clarifies things a bit.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367474" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-367474" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367474"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #27"></div>
  </section>
</div>
    <div class="postbit" id="367476" data-post-id="367476">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Nicd" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Nicd/120/2357_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Nicd
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="New2Elixir" data-post="27" data-topic="47323">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/n/dbc845/48.png" class="avatar"> New2Elixir:</div>
<blockquote>
<p>I read somewhere IIRC that this is done in part for safety/data corruption as with numerous processes this reduces the chance for one process to change a variable while another is working on it.</p>
</blockquote>
</aside>
<p>Processes in Elixir have isolated memory, and as such a process cannot alter a variable of another process.</p>
<aside class="quote no-group" data-username="New2Elixir" data-post="27" data-topic="47323">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/n/dbc845/48.png" class="avatar"> New2Elixir:</div>
<blockquote>
<p>What if my program were actually naively written similar to that, and I needed x to return 2?</p>
</blockquote>
</aside>
<p>You would need to do it in another way, reaching for process state, ETS, <code>:atomics</code>, <code>:counters</code>, <code>:persistent_term</code> or the process dictionary. It would completely depend on the situation and without an example situation it is not useful to suggest any particular technique.</p>
<p>From my personal experience, the situation where you actually want to use the mutable value is a minority case, and in the majority of cases you want to use the value as it was when the function was defined. This also applies when I’m writing in languages like JavaScript, where I have to spend time thinking if I’m now using a copy or a mutable reference if I’m writing a long-lived closure. In immutable languages like Elixir I have to spend a lot less time thinking about that.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367476" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-367476" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367476"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #28"></div>
  </section>
</div>
    <div class="postbit" id="367479" data-post-id="367479">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lud" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/120/14382_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lud
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="New2Elixir" data-post="27" data-topic="47323">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/n/dbc845/48.png" class="avatar"> New2Elixir:</div>
<blockquote>
<p>If after the last statement you were to simply print x though, it would print 2, right?</p>
</blockquote>
</aside>
<p>Yes, you can just reword “rebinding” as “creating a new variable with a recycled name”. It’s a new variable, but it’s given the same name as another one. The previous one is now hidden and can’t be accessed. In the fun body, the code is referring to the previous one and doesn’t “know” that the name is going to be recycled.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367479" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-367479" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367479"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #29"></div>
  </section>
</div>
    <div class="postbit" id="367482" data-post-id="367482">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’d explain (the benefits of) immutability differently:</p>
<pre data-code-wrap="js"><code class="lang-js">let array = [1, 2, 3];
let reversed = array.reverse();
</code></pre>
<p>In JS you cannot know just from reading that piece of code if <code>array</code> is now equal to <code>reversed</code> or still the original value assigned in the first line. You’d need to familiarize yourself with the implementation of <code>Array.prototype.reverse()</code>. In this case <code>reversed === array</code>, but code like that causes/caused enough problems that there’s also a <code>Array.prototype.toReversed()</code> nowadays, which returns a new array instead of modifying the existing one.</p>
<p>In elixir you can have:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">list = [1, 2, 3]
reversed = Enum.reverse(list)
</code></pre>
<p>No matter how <code>Enum.reverse/1</code> happens to be implemented it cannot make the <code>list</code> variable resolve to any value other than <code>[1, 2, 3]</code>. Essentially <code>immutability</code> is equal to having <code>pass by value</code> everywhere and no <code>pass by reference</code>. That’s the model upheld by the VM.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367482" data-batch-url="/posts/batch_likers">
                        14
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-367482" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367482"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #30"></div>
  </section>
</div>
    <div class="postbit" id="367515" data-post-id="367515">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="garrison" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  garrison
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sodapopcan" data-post="26" data-topic="47323">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>It’s also helpful to see example with complex data structures:</p>
</blockquote>
</aside>
<p>Your example wasn’t quite right, you are instantiating a new dict for <code>b</code> so it actually wouldn’t mutate the dict pointed to by <code>a</code>.</p>
<p>This should be correct:</p>
<pre data-code-wrap="python"><code class="lang-python"># Python
a = {'foo': 'bar'}
b = a
b['foo'] = 'hello world!'

print(a) # -&gt; {'foo': 'hello world!'}
print(b) # -&gt; {'foo': 'hello world!'}
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Elixir
a = %{"foo" =&gt; "bar"}
b = a
b = Map.put(b, "foo", "hello world!")
# Or: b = Map.put(a, "foo", ...)

dbg a # -&gt; %{"foo" =&gt; "bar"}
dbg b # -&gt; %{"foo" =&gt; "hello world!"}
</code></pre>
<p>We could get copy-on-write behavior (“immutability”) in Python by copying <em>manually</em>:</p>
<pre data-code-wrap="python"><code class="lang-python"># Python
a = {'foo': 'bar}
b = copy.deepcopy(a)
b['foo'] = 'hello world!'

print(a) # -&gt; {'foo': 'bar}
print(b) # -&gt; {'foo': 'hello world!'}
</code></pre>
<p>However, this is much less efficient because Elixir uses special persistent data structures underneath which duplicate as little memory as possible while maintaining the <em>illusion</em> of copy-on-write. If you had a map with a thousand keys and changed one, the other thousand would still point to the same memory as the last map, whereas the Python example would duplicate the entire thing. You <em>could</em> implement a persistent dict in Python, and I’m sure somebody has, but in Elixir it works that way <em>by default</em>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367515" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-367515" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367515"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/47323/load_more?page=4">Load more posts (10 remaining)</a>
</div></template></turbo-stream>