Understanding Return Values and None
The sort() method arranges list elements in ascending order by modifying the list in place.
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
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?
Before the call: The list contains [3, 1, 2].
Apply sort(): sort() rearranges the elements of the list into ascending order.
Read 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
Assigning the result of sort() back to the same variable.
sort() modifies the list but returns None. The assignment therefore overwrites the variable with None.
Fix:
Call the method without assignment: numbers.sort().Expecting the method-call expression to be the sorted list.
The sorted elements remain in numbers, while the return value of sort() is None.
Fix:
Use 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
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
- sort() arranges list elements in ascending order.
- sort() modifies the original list in place.
- A void method returns None rather than a new value.
- Call sort() without assignment when you want to modify the list.
- Writing t = t.sort() overwrites t with None.
Key Takeaways
- 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.