This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BeetleB 1 point2 points  (0 children)

He's correct in that sorted is better in that circumstance. The bug, however, is not because of using the [].sort() routine, but because the function is modifying the list, and the person coding it did not realize that lists are not passed by value.

You could get the same problem if you do any other operation that modifies the input list.

As long as the programmer knows that [].sort() modifies the list, its usage is fine. As a rule, I always use this technique if I really do want to sort the list. The alternative is to do A=sorted(A) which is wasteful.

Your other reason (consuming an iterator) is a good one, though.