title: PropertyMock with changing values after every call
body: <p>I'm trying to mock psycopg2 and need to handle cursor.description</p>
  
  <pre><code>  # run query 1
    column_names = [desc[0] for desc in cursor.description]
    # run query 2
    # NB cursor.description now returns different value
    column_names = [desc[0] for desc in cursor.description]
  </code></pre>
  
  <p>I can do this via a PropertyMock:</p>
  
  <pre><code>  type(fake_cursor).description = PropertyMock(return_value=descriptions[0])
  </code></pre>
  
  <p>but it doesn't solve my problem because the system under test does two queries and the description will be different for the second time.</p>
  
  <p>How can I change the return value of the mock on the second call?</p>
  
  <p>I tried:</p>
  
  <pre><code>cursor_execute_call_count = 0
  
  def handle_cursor_execute(arg1, arg2):
      cursor_execute_call_count = cursor_execute_call_count + 1
      type(fake_cursor).description = PropertyMock(return_value=descriptions[cursor_execute_call_count])
  
  fake_cursor.execute = Mock(side_effect=handle_cursor_execute)
  </code></pre>
  
  <p>But I get </p>
  
  <pre><code>E     UnboundLocalError: local variable 'cursor_execute_call_count' referenced before assignment
  </code></pre>
  
  <p>(That doesn't make sense to me. There must be a weird scoping issue going on.)</p>
  
  <p>I did eventually get it working with this code but it seems like there must be a better way:</p>
  
  <pre><code>def handle_cursor_execute(arg1, arg2):
    description = descriptions.pop()
    type(fake_cursor).description = PropertyMock(return_value=description)
  </code></pre>