Chained Conditions that require order to not produce order
<p>Whenever I <strong>chain conditions</strong> in Python (or any other language tbh) I stumble upon asking myself this, kicking me out of the productive "Zone". 
When I chain conditions I can, by ordering them correctly, check conditions that without checking for the other conditions first, may produce an Error.</p>

<p>As an example lets assume the following snippet:</p>

<pre class="lang-py prettyprint-override"><code>if "attr" in some_dictionary and some_value in some_dictionary["attr"]:
    print("whooohooo")
</code></pre>

<p>If the first condition wasnt in the first place or even absent, the second condition my produce an <code>KeyError</code></p>

<p>I do this pretty often to simply save space in the code, but I always wondered, if this is good style, if it comes with a risk or if its simply "pythonic".</p>

<p>A more Pythonic way is to "ask for forgivness rather than permission". In other words, use a try-except block:</p>

<pre><code>try:
    if some_value in some_dictionary["attr"]:
        print("Woohoo")
except KeyError:
    pass
</code></pre>

python condition keyerror