Disable python in-built function
<p>How could I disable a python in-built function?</p>

<p>For example, I note that it is possible to reassign or overwrite <code>len()</code> (like <code>len = None</code>), but it is not possible to reassign <code>list.__len__()</code> , which raises:</p>

<blockquote>
  <p>TypeError: can't set attributes of built-in/extension type 'list'</p>
</blockquote>

<p>However, even if reassignment were possible it seems <a href="http://stackoverflow.com/questions/20885760/how-to-get-back-an-overridden-python-built-in-function">easy to override</a>. In this example, <code>del len</code> or <code>from builtins import len</code> would restore the original functionality of <code>len()</code>.</p>

<p>The reason I ask is that on <a href="http://codewars.com/" rel="nofollow">Codewars</a> sometimes people want to set a coding challenge for a user to complete while forbidding the use of certain in-built functions. A trivial example could be determining the length of a list without using the length function.</p>

<p>On reflection, thanks to the comments I have already received and <a href="http://stackoverflow.com/questions/3068139/how-can-i-sandbox-python-in-pure-python">this related question</a>, I now realize that a full-proof solution is very hard, but I'd still be interested in a pragmatic solution that could be useful in this context.</p>

<p>Thank you</p>

<p><strong>Preface:</strong>
given the various comment conversations... It should be noted that none of these things are sufficient protection for running un-trusted code, and to be absolutely safe, you need a different interpreter with sandboxing specifically built in. <a href="http://stackoverflow.com/a/3068475/3220135">Here</a> is a previous answer discussing that</p>

<p>Given the example of writing a code wars question.. I would sub-class list, string, etc. with custom classes that disable the <code>__len__</code> function, then override the respective constructors with your own custom class.. make sure to provide the test case inputs as instances of the new class, as you cannot override the literal string/list constructors as they are linked to the interpreter directly..</p>

<p>for example:</p>

<pre><code>oldList = list
class myList(list):
    def __len__(self):
        raise AttributeError("'len' has been disabled for list objects")
list = myList
</code></pre>

python python-3.x