-3

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.

aafulei
  • 2,085
  • 12
  • 27
  • 1
    Actually, there's alternative `some_list = sorted(some_list)`. – Olvin Roght Oct 07 '19 at 12:20
  • 1
    the makers of python didn't want something to be returned _and_ a side effect to be performed. – Jean-François Fabre Oct 07 '19 at 12:20
  • 1
    You got it the other way around. Your quote says that if something returns `None` then it might be an inplace operation. You concluded the other way around, that an inplace operation **should** return `None`. A downside of returning `None` vs the object is that it makes chaining impossible, ie you can't do `list.append(1).sort()` – DeepSpace Oct 07 '19 at 12:22
  • Having the ability to distinguish between pure functions (like `sorted`) and functions with a side-effect (`list.sort`) is good for reducing complexity on large code bases. – rdas Oct 07 '19 at 12:22
  • 1
    You either get a result (return) or get something performed in place. Doing both at once is unexpected, you don't know what you get - while with that rule, you can see what you will get . Python relies on readability and understanding of the code, so you should always know what happens, no unexpected side effects. __It's a good practice to stick to that rule.__ However, you don't have to do that in your own code - I sometimes don't because I love method chaining and list comprehensions. But I document those functions for the future use. – h4z3 Oct 07 '19 at 12:26
  • @DeepSpace Do you want to say (the impossibility of) `list.sort().append(1)`? – aafulei Oct 07 '19 at 12:42
  • @h4z3 I am with you in that this may be more of a trade-off. You lose the convenience of chaining, but gain the clean code. Don't know if it's possible for you to make your comment an answer. – aafulei Oct 07 '19 at 12:45
  • @aafulei It's more of a discussion than a question itself, and there are many good answers here. ;) And I'm not here for points, I'm here for learning. – h4z3 Oct 07 '19 at 12:50
  • @h4z3 Unfortunately it looks like I cannot give you points, but I will take your point. – aafulei Oct 07 '19 at 12:58

1 Answers1

2

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

def foo(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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Hi. I don't know if I get you correctly. Say `my_list` has been sorted, and returned, and assigned to `my_list_2`. Both `my_list` and `my_list_2` are now sorted, referring to the same list. What could be the problem? – aafulei Oct 07 '19 at 12:51
  • 1
    That `my_list` is now also sorted when you thought you only sorted `my_list_2` and you don't notice until much later when you're chasing some subtle bug introduced by this behaviour which you weren't aware of. – deceze Oct 07 '19 at 13:01