title: Is it possible to mock the builtin len() function in Python 3.6?
body: <p>Is it possible to mock the builtin <code>len()</code> function in Python 3.6?</p>
  
  <p>I have a class which defines a simple method whic is dependent upon the <code>len()</code> function as follows:</p>
  
  <pre><code>class MyLenFunc(object):
      def is_longer_than_three_characters(self, some_string):
          return len(some_string) &gt; 3
  </code></pre>
  
  <p>I am trying to write a unit test for this method, but I am unable to mock out the <code>len()</code> function without errors being produced.
  Here's what I have so far:</p>
  
  <pre><code>import unittest
  from unittest.mock import patch
  import my_len_func
  
  
  class TestMyLenFunc(unittest.TestCase):
  
      @patch('builtins.len')
      def test_len_function_is_called(self, mock_len):
          # Arrange
          test_string = 'four'
  
          # Act
          test_object = my_len_func.MyLenFunc()
          test_object.is_longer_than_three_characters(test_string)
  
          # Assert
          self.assertEqual(1, mock_len.call_count)
  
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p>I found another SO question/answer <a href="https://stackoverflow.com/questions/192649/can-you-monkey-patch-methods-on-core-types-in-python/192857#192857">here</a> which suggests that it is not possible to mock out builtin functions because they are immutable.  However, I found a couple more websites <a href="http://www.voidspace.org.uk/python/mock/examples.html#partial-mocking" rel="noreferrer">here</a> and <a href="http://www.dsfcode.com/mock-pythons-builtin-functions/" rel="noreferrer">here</a> which suggest otherwise.  My attempt at the unit test class above is taken directly from the latter of these websites (and yes, I've tried the other techniques mentioned there. All end with the same error).</p>
  
  <p>The error I'm getting is rather long to post the full thing, so I'll snip out the repeating parts of it (you'll see that it's recursive from the final part of the error message).  The error text is as follows:</p>
  
  <pre><code>ERROR: test_len_function_is_called (__main__.TestMyLenFunc)
  ---------------------------------------------------------------------- 
  Traceback (most recent call last):
      File "C:\Python36\Lib\unittest\mock.py", line 1179, in patched
          return func(*args, **keywargs)
      File "C:/Python/scratchpad/my_len_func_tests.py", line 16, in test_len_function_is_called
          test_object.is_longer_than_three_characters(test_string)
      File "C:\Python\scratchpad\my_len_func.py", line 3, in is_longer_than_three_characters
          return len(some_string) &gt; 3
      File "C:\Python36\Lib\unittest\mock.py", line 939, in __call__
          return _mock_self._mock_call(*args, **kwargs)
      File "C:\Python36\Lib\unittest\mock.py", line 949, in _mock_call
          _call = _Call((args, kwargs), two=True)
      File "C:\Python36\Lib\unittest\mock.py", line 1972, in __new__
          _len = len(value)
      ...
      (Repeat errors from lines 939, 949, and 1972 another 95 times!)
      ...
      File "C:\Python36\Lib\unittest\mock.py", line 939, in __call__
          return _mock_self._mock_call(*args, **kwargs)
      File "C:\Python36\Lib\unittest\mock.py", line 944, in _mock_call
          self.called = True RecursionError: maximum recursion depth exceeded while calling a Python object
  
  ----------------------------------------------------------------------
  Ran 1 test in 0.004s
  
  FAILED (errors=1)
  </code></pre>
  
  <p>Many thanks in advance.</p>