4

How could I disable a python in-built function?

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

TypeError: can't set attributes of built-in/extension type 'list'

However, even if reassignment were possible it seems easy to override. In this example, del len or from builtins import len would restore the original functionality of len().

The reason I ask is that on Codewars 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.

On reflection, thanks to the comments I have already received and this related question, 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.

Thank you

13
  • 4
    The global name __len__ and an object's attribute named __len__ are two completely different things. Commented Sep 6, 2016 at 18:31
  • 4
    __len__ is an attribute of the list type, while your example assigns a new variable to the global namespace. The right namespace would be list.__len__ = None, but you can't easily do that with types implemented in C. Commented Sep 6, 2016 at 18:32
  • If you wanted to overwrite the object attribute __len__ you would have to do so for every object Commented Sep 6, 2016 at 18:32
  • 1
    @DeepSpace: Try it; you'll find that doesn't work. Commented Sep 6, 2016 at 18:33
  • 1
    You could try forbiddenfruit. I'm not having much luck on it with my anaconda installation but it looks promising. Commented Sep 6, 2016 at 20:07

1 Answer 1

2

Preface: 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. Here is a previous answer discussing that

Given the example of writing a code wars question.. I would sub-class list, string, etc. with custom classes that disable the __len__ 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..

for example:

oldList = list
class myList(list):
    def __len__(self):
        raise AttributeError("'len' has been disabled for list objects")
list = myList
Sign up to request clarification or add additional context in comments.

11 Comments

downvoted just for the fun of it or is someone going to explain why this is a bad solution to Chris' problem?
I think they gave you downvote because of the same reason I've got before edited my question, reason was: OP asks not how to replace, but how to remove __len__.
@PadraicCunningham Like I already said... you can't override literal constructors, but if this is for code wars, you simply provide the people answering the questions an instance of the custom class as an input. They can't provide literal constructors for every possible input value within the body of the function..
Also, with this approach the user can del list to regain access to the original. As a workaround you can assign __builtins__.list = myList but that still won't solve the problem @PadraicCunningham mentioned
@Aaron Clearly they're not going to be happy unless you write a fully functional variant of CPython implementing the requested behavior :P
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.