title: Using 'try' vs. 'if' in Python
body: <p>Is there a rationale to decide which one of <code>try</code> or <code>if</code> constructs to use, when testing variable to have a value?</p>
  <p>For example, there is a function that returns either a list or doesn't return a value. I want to check result before processing it. Which of the following would be more preferable and why?</p>
  <pre><code>result = function();
  if (result):
      for r in result:
          #process items
  </code></pre>
  <p>or</p>
  <pre><code>result = function();
  try:
      for r in result:
          # Process items
  except TypeError:
      pass;
  </code></pre>
  <h3>Related discussion:</h3>
  <p><em><a href="https://stackoverflow.com/questions/204308/">Checking for member existence in Python</a></em></p>