I am preparing an exam question that involves sorting. I would like to prohibit students to use list.sort()
in their solution. For that reason I was thinking of overriding that method (for example by a method that throws a warning). Is there a way that can be done easily in Python 3? Using reflection? (Or another solution so that list.sort()
cannot be used by the students in their code.)
Asked
Active
Viewed 236 times
2
-
1Even if you managed to remove/replace `list.sort` what prevents your students from doing the following: `ast.literal_eval(subprocess.check_output([sys.executable, '-Ic', f'print(sorted({the_list!r}))']).decode())` (let's hope they don't read this chat). The only way to be certain is to compile your own version of Python. Anyway I think if they come up with `list.sort` they should be given full credit since that's the most Pythonic solution :-) If you want to teach low-level algorithms you should perhaps use a low-level language. At least for the sake of Python's future user base. – a_guest May 13 '20 at 14:18
-
1https://stackoverflow.com/questions/6738987/extension-method-for-python-built-in-types should be helpful. Typically you will get the error `can't set attributes of built-in/extension type 'list'` if you try to set `list.sort = my_sort_func`, but it seems that there are other solutions out there. – jkr May 13 '20 at 14:21
-
1There exists a project ([Forbidden Fruit](https://github.com/clarete/forbiddenfruit)) which allows you to monkey-patch builtin types in CPython. Obviously there are ways to revert these monkey-patches so it's not 100% safe (+ it's not a global change so invoking another subprocess sidesteps this easily). – a_guest May 13 '20 at 14:21
-
@a_guest indeed that module Forbidden Fruit looks interesting. – Kim Mens May 13 '20 at 15:03
-
@jakub that question indeed also points to the Forbidden Fruit module – Kim Mens May 13 '20 at 15:05
-
1@a_guest We are talking about first year students here. If the manage to do things like ```ast.literal_eval(subprocess.check_output([sys.executable, '-Ic', f'print(sorted({the_list!r}))']).decode())``` they should probably pass the course :-) – Kim Mens May 13 '20 at 15:06
-
@KimMens - i don't know the specifics here, but why don't you simply tell them not to use `list.sort`? and if they use it, take points off? changing the behavior of builtin methods seems like overkill when you can put the requirement in the question. – jkr May 13 '20 at 15:14
-
@jakub We will tell them not to use list.sort of course. But after that we will test their code with an autograder that should run its tests automatically. In that test I want to make sure that list.sort cannot be used. – Kim Mens May 13 '20 at 18:12
-
@KimMens - perhaps a solution then would be to search for `.sort()` in their solutions? i don't really understand why you need to fully restrict access to `.sort`. – jkr May 13 '20 at 18:26
2 Answers
0
In the end we went for this solution pointed out by a colleague, simplified here for the sake of the example. The curse
function from forbiddenfruit
allows you to simply override an existing function like sort
:
from forbiddenfruit import curse
def f ( v ):
print ( "Error" )
curse(list,"sort", f)
l = [5,4,3,1]
l.sort ()
print (l)
So in this piece of code where we replaced sort
by a function that prints an error the students cannot rely on the traditional sort
anymore.

Kim Mens
- 325
- 1
- 13
-1
Sure, but as far as I know you'll have to make your students use a custom list class instead of the standard one. If that's ok with what you want to do, you can do it as:
import random
class CustomList(list):
def sort(self, *, key=..., reverse: bool = ...) -> None:
raise PermissionError("You're not permitted to use the built-in sort function")
if __name__ == '__main__':
lst = CustomList()
for _ in range(0, 10):
lst.append(random.randint(0, 100))
lst.sort()
Note that your students won't be able to do lst = [5, 3, 7, 1, 9]
syntax with this implementation since that'd call the default built-in list implementation.

Roäc
- 149
- 19
-
1@a_guest I strongly disagree with what you're trying to imply with your message. As a computer sciences teacher I know that if you set conditions like this for assignments students will (in most cases) follow these rules. And it's much easier to check that they used the ``CustomList`` class than if they used the sort function. – Roäc May 13 '20 at 14:39
-
1@Roäc I really want the solution to work with standard lists like ```lst = [3,7,1,5,9]``` because it is a question on list manipulation. And the reason for disallowing list.sort() is because the exercise I have in mind has a very short but very inefficient solution using sort(). Also, that solution wouldn't be testing the competences that I want to evaluate in this exercise. – Kim Mens May 13 '20 at 15:10
-
1@Roäc I didn't mean to imply that their intention is to cheat on the exercise. Rather such a restriction will make them explicitly aware of that existing solution and the fact that they're not allowed to use it, even though it's right at their hands, can be (i.m.o.) a frustrating experience. If I can imagine a solution using `list.sort` which is extremely concise I wouldn't want to work around it by writing ten times more code. Working with Python means to use the entire toolbox. Restricting parts of it gives a wrong feel for the language. – a_guest May 13 '20 at 16:04
-
@KimMens I'm curious about this exercise because Python's sorting algorithm is very efficient. If it's really inefficient in this case why not challenge your students to beat that solution with a more efficient one? Python offers modules with which you can easily benchmark code and that way they'd be naturally motivated (and required) to come up with something different than just `list.sort`. – a_guest May 13 '20 at 16:07
-
@KimMens ok, I get it. Could you give a little more details about what is the problem you want them to solve? Maybe that can be helpful for someone to give you an answer according to your needs – Roäc May 13 '20 at 16:10
-
@a_guest sorry, your comment seemed a little bit sarcastical to me. Now that you explained what you meant I understand better what you wanted to tell. – Roäc May 13 '20 at 16:11
-
@Roac unfortunately I can not divulge any details on the problem I want them to solve here, for obvious reasons. In fact it is not a sorting problem though, but you could preprocess the list by sorting it first and then traverse it. – Kim Mens May 13 '20 at 18:16
-
@KimMens There are various options as mentioned in the comments and this answer. It depends what level of certainty you want. The only 100% safe solution is to compile your own version of Python. All others can be worked around though the level of difficulty varies. If sorting the data makes the problem easier to solve I don't understand why you want to restrict this. You cannot prevent your students from doing so anyway, since code for various sorting algorithms is available online. If sorting is really inefficient in that case then benchmarking their solution against `list.sort` should work. – a_guest May 13 '20 at 22:06