title: How to do basic dependency injection in Python (for mocking/testing purposes)
body: <p>Python is a relatively new language for me. Unit Testing and Dependency Injection are something that I've been doing for a little while now, so I'm familiar with it from a C# perspective.</p>
  
  <p>Recently, I wrote this piece of Python code:
  </p>
  
  <pre><code>import requests  # my dependency: http://docs.python-requests.org/en/latest/
  
  class someClass:
      def __init__(self):
          pass
  
      def __do(self, url, datagram):
          return requests.post(self, url, datagram)
  </code></pre>
  
  <p>And then I realized that I had just created a hard-coded dependency. Bleh.</p>
  
  <p>I had considered changing my code to do "Constructor" Dependency Injection:</p>
  
  <pre><code>def __init__(self,requestLib=requests):
      self.__request = requestLib
  
  def __do(self, url, datagram):
      return self.__request.post(self, url, datagram)
  </code></pre>
  
  <p>This now allows me to inject a fake/mock dependency for the sake of Unit Testing, but wasn't sure if this was considered Python-ic. So I'm appealing to the Python community for guidance. </p>
  
  <p>What are some examples of Python-ic ways to do basic DI (mostly for the sake of writing Unit Tests that utilize Mocks/Fakes)?</p>
  
  <p><strong>ADDENDUM</strong> For anyone curious about the Mock answer, I decided to ask a separate question here: <a href="https://stackoverflow.com/questions/33353855/how-does-mock-patch-know-which-parameter-to-use-for-each-mock-object">How does @mock.patch know which parameter to use for each mock object?</a> </p>