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 python function returns None, it is a sign, that the operations are done in place, that's why, when you want to print list.sort() it returns None.
I am wondering the reason for the language-design principle that an in-place-modifying method should return None
. Just for curiosity, say, why cannot list.sort()
(and other in-place-modifying functions/methods) return itself?
A possbile argument is that doing so avoids creating a copy and could be faster. However, copying a list in Python only creates a reference and shouldn't be much expensive.
Another argument could be list.sort()
, from its literal meaning, shouldn't be an object. Then what about having another method that both modifies list in place and returns a copy -- call it list.sorted()
? I guess having such a method will facilitate certain usage.