title: Python getattr() with another attribute as the default
body: <p>I have two classes. One class has an attribute <code>x</code> but not <code>y</code>, and the other class has an attribute <code>y</code> but not <code>x</code>.</p>
  
  <p>I have a function that accepts either class as an argument. Is there a one-line way to assign a new variable to the <code>x</code> attribute if it exists, or the <code>y</code> attribute if not? I.e.,</p>
  
  <pre><code>if hasattr(input_object, 'x'):
      new_var = input_object.x
  else:
      new_var = input_object.y
  </code></pre>
  
  <p>I thought I would be able to do:</p>
  
  <pre><code>new_var = getattr(input_object, 'x', input_object.y)
  </code></pre>
  
  <p>But that raises an <code>AttributeError</code> if the <code>input_object</code> doesn't have <code>y</code>, even though it does have <code>x</code>.</p>