Popular recipes by Andreas Nilsson http://code.activestate.com/recipes/users/4167183/2008-10-04T14:39:20-07:00ActiveState Code Recipesfreeze(), 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> compare(), making filter() fun again (Python) 2008-10-04T12:40:42-07:00Andreas Nilssonhttp://code.activestate.com/recipes/users/4167183/http://code.activestate.com/recipes/576526-compare-making-filter-fun-again/ <p style="color: grey"> Python recipe 576526 by <a href="/recipes/users/4167183/">Andreas Nilsson</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>). Revision 2. </p> <p>compare() takes a function parameter and returns a callable comparator when compared to a value. When called, the comparator returns result of comparing the result of calling the function and the value it was created with.</p> <p>Basic usage:</p> <pre class="prettyprint"><code>items = [1, 2, 3, 4, 5] def double(x): return x * 2 for i in filter(compare(double) &gt; 5, items): print i #Prints 3, 4 and 5 </code></pre>