<div class="gmail_quote">On Fri, Jun 8, 2012 at 9:34 AM, Paul Barry <span dir="ltr"><<a href="mailto:paul.james.barry@gmail.com" target="_blank">paul.james.barry@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I've been coding/working with Erlang (and Chicago Boss) for a while<br>
now, and I've recently had to do some work in Python.  I find myself<br>
looking at some code I'm writing that looks like this:<br>
<br>
   html = html + "some markup"<br>
<br>
and getting nervous as I know this is a "naughty" that Erlang would<br>
not let me away with.   If only there was a "make everything<br>
immutable" switch for Python!  ;-)</blockquote><div><br></div><div>Strictly speaking, there's no mutation at all here, because str in Python *are* immutable. What's happening is that the variable html is rebound to the result. This is roughly equivalent to something like this in Erlang [1]:</div>
<div><br></div><div>    HTML1 = <<HTML/binary, "some markup">>.</div><div><br></div><div>You really wouldn't want to have to write Python in single assignment form, especially without tail call optimization. Python IO would benefit from something like Erlang's iolist, but for use cases like the above people tend to append to a list and then join it with the empty string, or use an instance of cStringIO.StringIO.</div>
<div><br></div><div>[1] Some versions of CPython do have some hacks that can detect this x += s where x is a string with a reference count of 1 and strategically do these updates in-place with over-allocation... so it's possible that it's not quite as bad as you think it is (amortized).</div>
<div><br></div><div>-bob</div><div><br></div></div>