title: Number of ways to partition a number in Python
body: <p>I have defined a recursive function that takes a number, <code>n</code>, and returns a <code>list</code> of lists of the numbers that sum to that number (partitions):</p>
  
  <pre><code>def P(n):
      # base case of recursion: zero is the sum of the empty list
      if n == 0:
          yield []
          return
  
      for p in P(n-1):        
          p.append(1)
          yield p
          p.pop()
          if p and (len(p) &lt; 2 or p[-2] &gt; p[-1]):
              p[-1] += 1
              yield p
  </code></pre>
  
  <p>I was wondering how to make the function return the <em>number</em> of partitions for number <code>n</code>.</p>
  
  <p>For example, <code>P(6)</code> would return <code>10</code>.</p>