title: Python getattr equivalent for dictionaries?
body: <p>What's the most succinct way of saying, in Python, "Give me <code>dict['foo']</code> if it exists, and if not, give me this other value <code>bar</code>"? If I were using an object rather than a dictionary, I'd use <code>getattr</code>:</p>
  
  <pre><code>getattr(obj, 'foo', bar)
  </code></pre>
  
  <p>but this raises a key error if I try using a dictionary instead (a distinction I find unfortunate coming from JavaScript/CoffeeScript). Likewise, in JavaScript/CoffeeScript I'd just write</p>
  
  <pre><code>dict['foo'] || bar
  </code></pre>
  
  <p>but, again, this yields a KeyError. What to do? Something <em>succinct</em>, please!</p>