title: Recursivly looping through vectors with generators in Python
body: <p>The following python-code uses recursion to print all lists of nonnegative integers of length 3 whose sum is 2, it works as intended:</p>
  <blockquote>
  <pre><code>def rek(f,sum,n,vector=[]): #applies f to all Z^n_+ vectors of sum 'sum'
      if n==1:
          f(vector+[sum])
      else:        
          for i in range(sum+1):
              rek(f,sum-i,n-1,vector+[i])
                     
  rek(print,2,3)
  </code></pre>
  </blockquote>
  <pre><code>Output: 
  
   [0, 0, 2]  [0, 1, 1]  [0, 2, 0]  [1, 0, 1]  [1, 1, 0]  [2, 0, 0]
  </code></pre>
  <p>My question is if and how I could do this with generators instead? I would like to be able to write something like</p>
  <blockquote>
  <pre><code>for vector in vector_generator(2,3):
      print(vector)
  </code></pre>
  </blockquote>
  <p>to print the same vectors.</p>