title: Does python have a shorthand to check if an object has an attribute?
body: <p>Background is I'm getting data from a JSON API where lots of fields are optional and I want most of the fields in a database. When a specific field isn't available I want an empty string (<code>""</code>) written into the database.</p>
  
  <p>Currently I've been doing:</p>
  
  <pre><code>if jsonobject.what_i_look_for:
    dbstring = jsonobject.what_i_look_for
  else:
    dbstring = ""
  </code></pre>
  
  <p>And then inserted dbstring into the database. However I'm getting a lot more of these fields now and I want a much cleaner code rather than a function which consists about 80% of if-statements.</p>
  
  <p>I've found <a href="https://stackoverflow.com/a/14461984/9138821">if-shorthands</a> and <a href="https://stackoverflow.com/a/13710667/9138821">this</a> shorthand to check if a variable is empty, but both don't seem to work directly as a string. I've tested this using <code>print()</code> in an interactive python 3.5.2 shell:</p>
  
  <pre><code>&gt;&gt;&gt; print(testvar or "")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  NameError: name 'testvar' is not defined
  
  &gt;&gt;&gt; print(testvar if testvar else "")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  NameError: name 'testvar' is not defined
  </code></pre>
  
  <p>This: <code>echo (isset($testvar) ? $testvar : "");</code> is the PHP equivalent of what I seek.</p>
  
  <p><strong>Edit:</strong> Since it seems relevant: The object I am trying to process is coming from Telegram's JSON API. I'm using <code>python-telegram-bot</code> as library and <a href="https://python-telegram-bot.readthedocs.io/en/stable/telegram.chat.html" rel="noreferrer">this</a> is an example object.</p>