title: setattr in monkeypatching of a function throws attribute error
body: <p>I have the following code</p>
  
  <p><code>/tests/test_fixme.py</code></p>
  
  <pre><code>def make_my_patch():
      name = "Dinesh"
      return name
  
  
  def test_make_patch(monkeypatch):
  
      monkeypatch.setattr(make_my_patch,"name","DineshKumar")
  </code></pre>
  
  <p>Using <code>setattr</code> to set <code>name</code> throws an attribute error.</p>
  
  <p>Note : I have placed both the functions in the same file as a stop gap solution to avoid module/import errors for now.</p>
  
  <p>Here is the traceback.</p>
  
  <pre><code>================================================================== FAILURES ==================================================================
  ______________________________________________________________ test_make_patch _______________________________________________________________
  
  monkeypatch = &lt;_pytest.monkeypatch.MonkeyPatch object at 0x7fddd5675630&gt;
  
      def test_make_patch(monkeypatch):
  
  &gt;       monkeypatch.setattr(make_my_patch,"name","DineshKumar")
  E       AttributeError: &lt;function make_my_patch at 0x7fddd5676048&gt; has no attribute 'name'
  
  tests/test_fixme.py:16: AttributeError
  ========================================================== short test summary info ===========================================================
  FAILED tests/test_fixme.py::test_make_patch - AttributeError: &lt;function make_my_patch at 0x7fddd5676048&gt; has no attribute 'name'
  ============================================================= 1 failed in 0.02s ==============================================================
  (testenv) user@user:~/MyGitHub/MyLearning/Python/MyUnitTests/Testing10$ 
  </code></pre>
  
  <p>How do I patch the name attribute correctly? However if I place my code inside a class, the setattr for class variable works just fine? Could you please explain why doesnt it work for functions?</p>