Popular recipes tagged "expression"http://code.activestate.com/recipes/tags/expression/2011-12-17T20:03:26-08:00ActiveState Code Recipes(Yet another) Assignment in expression Recipe (Python)
2011-12-17T20:03:26-08:00harish anandhttp://code.activestate.com/recipes/users/4180291/http://code.activestate.com/recipes/577987-yet-another-assignment-in-expression-recipe/
<p style="color: grey">
Python
recipe 577987
by <a href="/recipes/users/4180291/">harish anand</a>
(<a href="/recipes/tags/expression/">expression</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>Python does not support assignment in if and while statements such as "if (x=func()):". This is an attempt to bring similar functionality to python by injecting bytecode to all functions and methods in a module.
This recipe is inspired from recipes <a href="http://code.activestate.com/recipes/66061">66061</a>, <a href="http://code.activestate.com/recipes/202234-assignment-in-expression">202234</a> and <a href="http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time">277940</a>.</p>
Exception handling in a single line (Python)
2009-10-09T01:07:52-07:00Radek Szklarczykhttp://code.activestate.com/recipes/users/2847011/http://code.activestate.com/recipes/576872-exception-handling-in-a-single-line/
<p style="color: grey">
Python
recipe 576872
by <a href="/recipes/users/2847011/">Radek Szklarczyk</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/expression/">expression</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 16.
</p>
<p>The rules of <em>duck typing</em> in python encourage programmers to use the "try...except..." clause. At the same time python with new versions enables to use more powerful list comprehensions (for example Conditional Expressions). However, it is impossible to write the "try...except..." clause in a list comprehension. The following recipe "protects" a function against exception and returns a default value in the case when exception is thrown.</p>