title: How to patch a constant in Python using a mock as function parameter
body: <p>I'm trying to understand the different ways to patch a constant in Python using <em>mock.patch</em>.
  My goal is to be able to use a variable defined in my Test class as the patching value for my constant.</p>
  
  <p>I've found this question which explains how to patch a constant:
  <a href="https://stackoverflow.com/questions/27252840/how-to-patch-a-constant-in-python">How to patch a constant in python</a>
  And this question which explains how to use self in patch: 
  <a href="https://stackoverflow.com/questions/26979285/using-self-in-python-patch-decorator">using self in python @patch decorator</a></p>
  
  <p>But from this 2nd link, I cannot get the <em>testTwo</em> way (providing the mock as a function parameter) to work</p>
  
  <p>Here is my simplified use case:</p>
  
  <p><em>mymodule.py</em></p>
  
  <pre><code>MY_CONSTANT = 5
  
  def get_constant():
      return MY_CONSTANT
  </code></pre>
  
  <p><em>test_mymodule.py</em></p>
  
  <pre><code>import unittest
  from unittest.mock import patch
  
  import mymodule
  
  class Test(unittest.TestCase):
  
      #This works
      @patch("mymodule.MY_CONSTANT", 3)
      def test_get_constant_1(self):
          self.assertEqual(mymodule.get_constant(), 3)
  
      #This also works
      def test_get_constant_2(self):
          with patch("mymodule.MY_CONSTANT", 3):
              self.assertEqual(mymodule.get_constant(), 3)
  
      #But this doesn't
      @patch("mymodule.MY_CONSTANT")
      def test_get_constant_3(self, mock_MY_CONSTANT):
          mock_MY_CONSTANT.return_value = 3
          self.assertEqual(mymodule.get_constant(), 3)
          #AssertionError: &lt;MagicMock name='MY_CONSTANT' id='64980808'&gt; != 3
  </code></pre>
  
  <p>My guess is I shoudln't use <strong>return_value</strong>, because <em>mock_MY_CONSTANT</em> is not a function. So what attribute am I supposed to use to replace the value returned when the constant is called ?</p>