Popular recipes tagged "freeze"http://code.activestate.com/recipes/tags/freeze/2011-10-07T03:59:45-07:00ActiveState Code RecipesA Protocol for Making Objects Immutable (Python) 2011-10-07T03:59:45-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577895-a-protocol-for-making-objects-immutable/ <p style="color: grey"> Python recipe 577895 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/freeze/">freeze</a>, <a href="/recipes/tags/immutable/">immutable</a>, <a href="/recipes/tags/mutable/">mutable</a>, <a href="/recipes/tags/unfreeze/">unfreeze</a>). </p> <p>Python already provides immutable versions of many of the mutable built-in types. Dict is the notable exception. Regardless, here is a protocol that objects may implement that facilitates turning immutable object mutable and vice-versa.</p> freeze(), make any object immutable (Python) 2008-10-04T14:39:20-07:00Andreas Nilssonhttp://code.activestate.com/recipes/users/4167183/http://code.activestate.com/recipes/576527-freeze-make-any-object-immutable/ <p style="color: grey"> Python recipe 576527 by <a href="/recipes/users/4167183/">Andreas Nilsson</a> (<a href="/recipes/tags/const/">const</a>, <a href="/recipes/tags/freeze/">freeze</a>, <a href="/recipes/tags/immutable/">immutable</a>). Revision 2. </p> <p>Calling freeze() on an object makes the object immutable, like const in C++. Useful if you want to make sure that a function doesn't mess with the parameters you pass to it.</p> <p>Basic usage:</p> <pre class="prettyprint"><code>class Foo(object): def __init__(self): self.x = 1 def bar(f): f.x += 1 f = Foo() bar(freeze(f)) #Raises an exception </code></pre>