Understanding Return Values and None
The returned None is not the sorted list; the sorted elements are already in the list that was modified.
A Two-Part Result
A method call can affect an object and also produce a return value. With sort(), these two results must be kept separate: the list is rearranged in ascending order, while the method returns None. Understanding that difference prevents a particularly damaging assignment mistake.
What do you think happens?
After calling sort() on a list, what should you expect?
Reveal answer
The list is arranged in ascending order and the method returns None
sort() modifies the list in place. It does not return a new sorted value; its return value is None.
Sorting In Place
The sort() method arranges the elements of a list in ascending order by modifying that list in place. In place means that the original list is changed directly. The list does not need to be replaced by a separate sorted result for its elements to become ordered.
numbers is now [1, 2, 3].The Meaning of None
A void method modifies an object and returns None rather than returning a new value. For sort(), the list modification is the useful effect, and None is the method's return value.
This gives sort() two observable outcomes. The list's elements are now in ascending order, and evaluating the method call produces None. The returned None is not the sorted list; the sorted elements are already in the list that was modified.
Tracing a Void Method
Suppose numbers starts as [3, 1, 2]. What are the list contents after numbers.sort(), and what value does the method call return?
- 1Before the call
The list contains [3, 1, 2].
- 2Apply sort()
sort()rearranges the elements of the list into ascending order. - 3Read the method result
Because
sort()is a void method, its return value is None.
The list contains [1, 2, 3], and the method-call expression has the value None.
The Assignment Trap
numbers = [3, 1, 2]
numbers.sort()numbers is modified directly and becomes [1, 2, 3].
numbers = [3, 1, 2]
numbers = numbers.sort()sort() still modifies the list, but assignment stores its return value, None, in numbers.
Common mistake Assigning the result of sort() back to the same variable.
What it looks likenumbers = numbers.sort()Why it failssort()modifies the list but returns None. The assignment therefore overwrites the variable with None.Do this insteadCall the method without assignment: numbers.sort().
Common mistake Expecting the method-call expression to be the sorted list.
What it looks likesorted_numbers = numbers.sort()Why it failsThe sorted elements remain in numbers, while the return value of
sort()is None.Do this insteadUse numbers.sort() when you want to modify numbers directly, and do not assign its return value.
Practice the Trace
A list named values contains [4, 2, 3]. Trace the statement values.sort(). State the final contents of values and the return value of the method call. Then explain why values = values.sort() is not the correct replacement.
Hints
- Separate the list's changed state from the method's return value.
sort()arranges the elements in ascending order.- The method returns None, so assigning its result changes the variable.
What do you think happens?
After values.sort(), what are the two results to record?
Reveal answer
values is [2, 3, 4], and the method result is None
sort() changes the list in place and returns None. Assignment is the operation that would overwrite the variable with that None value.
Key Takeaways
- 1
sort()arranges list elements in ascending order. - 2
sort()modifies the original list in place. - 3A void method returns None rather than a new value.
- 4Call
sort()without assignment when you want to modify the list. - 5Writing t =
t.sort()overwrites t with None.
At a glance
sort()rearranges a list into ascending order by modifying the list in place.- The list changes, but the return value of
sort()is None. - Void methods should be called without assigning their return value.
- The expression t =
t.sort()overwrites the variable with None.