Skip to content Skip to sidebar Skip to footer

Why In Principle An In-place-modifying Method Should Return None

A language design question. Taking Python as example. Quoting a comment in an answer to a question concerning the difference between list.sort() and sorted(): In general, when a p

Solution 1:

If list.sort would return a copy of itself, programmers not knowing that the sorting happens in place would happily write code like this:

deffoo(my_list):
    return my_list.sort()

Since this works, they might be under the impression that the sorting happens only on a copy of the list, until some day they realise that they've been introducing subtle bugs into their code all along.

By denying this code to work in the first place, that ambiguity and a source of bugs is removed from the language entirely.

Post a Comment for "Why In Principle An In-place-modifying Method Should Return None"