title: Recursive generator for flattening nested lists
body: <p>I'm a programming newbie and am having some trouble understanding an example from my python textbook ("Beginning Python" by Magnus Lie Hetland). The example is for a recursive generator designed to flatten the elements of nested lists (with arbitrary depth):</p>
  
  <pre><code>def flatten(nested):
      try:
          for sublist in nested:
              for element in flatten(sublist):
                  yield element
      except TypeError:
          yield nested
  </code></pre>
  
  <p>You would then feed in a nested list as follows:</p>
  
  <pre><code>&gt;&gt;&gt; list(flatten([[[1],2],3,4,[5,[6,7]],8]))
  [1,2,3,4,5,6,7,8]
  </code></pre>
  
  <p>I understand how the recursion within flatten() helps to whittle down to the innermost element of this list, '1', but what I don't understand is what happens when '1' is actually passed back into flatten() as 'nested'. I thought that this would lead to a TypeError (can't iterate over a number), and that the exception handling was what would actually do the heavy lifting for generating output... but testing with modified versions of flatten() has convinced me that this isn't the case. Instead, it seems like the 'yield element' line is responsible.</p>
  
  <p>That said, my question is this... how can 'yield element' ever actually be executed? It seems like 'nested' will either be a list - in which case another layer of recursion is added - or it's a number and you get a TypeError. </p>
  
  <p>Any help with this would be much appreciated... in particular, I'd love to be walked through the chain of events as flatten() handles a simple example like:</p>
  
  <pre><code>list(flatten([[1,2],3]))
  </code></pre>