Working with Mutable and Immutable Types
Class methods belong to specific classes and are called using dotted notation on objects of that class.
One Operation, Two Kinds of Change
Python operations do not all handle data in the same way. A list can be changed directly, while a string operation creates a new string. That difference matters whenever more than one variable refers to the same list or when you expect a method call to produce a value.
What do you think happens?
Suppose two variables refer to the same list. If one variable is used with append, what should the other variable show afterward?
Reveal answer
Answer: The other variable shows the modified list.
A list is mutable. An in-place method changes the original list object, so every variable referring to that object sees the change.
Tracking the Original Object
A mutable object can be modified in place. Lists are mutable, so methods such as append() and sort() change the original list object rather than creating a separate list that you must assign back. The variable continues to refer to that same list object, but the contents of the object have changed.
After the append call, both items and other_name refer to the list whose contents are [1, 2, 3, 4]. Assigning another variable to the list does not create an independent list in this situation; both variables refer to the same list object.
Methods and Their Owners
A method is a function defined specifically for a class. For a list object, its methods are accessed with dotted notation, such as mylist.append(item). A standalone function is not tied to one particular object in that way.
The dot in mylist.append(item) connects the object to a method available through that object's class. This is why the list name appears before the dot and the method name appears after it. The source describes the corresponding standalone-function form append(mylist, item) as different from the method form because append is being accessed as part of the list object's available methods.
Return Values from In-Place Methods
An in-place list method has two effects that must be kept separate. First, it changes the original list. Second, it returns None rather than returning a new list. The sort() method follows this pattern: it changes the list directly and returns None.
numbers = [3, 1, 2] result = numbers.sort()
numbers is [1, 2, 3]
result is NoneStrings Follow a Different Pattern
Strings are immutable types. In the source's comparison, operations on strings always create new string objects rather than changing the original string in place. This contrasts with list methods such as append() and sort(), which modify the list where it exists.
In this example, the string operation produces a new string value for new_word. The original string referred to by word is not modified in place. The important question is therefore: does the operation change the existing object, as list mutation does, or produce a new object, as string operations do?
| Feature | List | String |
|---|---|---|
| Mutability | Mutable | Immutable |
| Effect of the operation described in the source | The original list can be changed in place | A new string is created |
| Example operation | append() or sort() | A string operation |
| Return behavior for the listed in-place methods | append() and sort() return None | String operations create a new string |
Finding Methods with help
When you need to discover what operations a list supports, call help(list). The help() function displays documentation for the list class, including its available methods and descriptions of what those methods do. This is a practical way to explore list capabilities instead of trying to remember every method name.
Mistakes Beginners Make
Expecting sort() to return a sorted list.
sort() changes the original list in place and returns None.
Fix:
Treat the list itself as the changed result and do not expect the method call to provide a new list.Assuming a second variable holds an independent list.
Multiple variables can refer to the same list object, so a change through one variable is visible through the other.
Fix:
Remember that an in-place change affects all variables referring to that list.Treating a string operation like a list mutation.
Strings are immutable, so string operations create new strings rather than modifying the original string in place.
Fix:
Expect a new string value from a string operation and distinguish that behavior from list mutation.Calling a list method as though it were a detached operation.
List methods are accessed through the list object using dotted notation.
Fix:
Use the object.method() pattern for methods belonging to the object's class.
Practice the Prediction
For each situation, predict the state of the variables and the method return value before checking your reasoning: a list is assigned to two variables and append() is called through one of them; a list is passed to sort() and the call is assigned to another variable; a string operation is used and its result is assigned to a new variable.
Hints
- Ask whether the object is mutable or immutable.
- For a list method, separate the change to the list from the value returned by the method.
- Check whether both variables refer to the same list.
- Identify the object receiving the dotted method call.
- Decide whether the object is a mutable list or an immutable string.
- If the object is a list, determine whether the method changes it in place.
- Record the method's return value separately from the object's new contents.
- Check whether another variable refers to the same list object.
Key Takeaways
- Methods belong to specific classes and are accessed through dotted notation on objects.
- Lists are mutable, so append() and sort() can change the original list in place.
- In-place list methods change the list and return None.
- Multiple variables can refer to the same list, so a change through one variable affects the others.
- Strings are immutable, so string operations create new strings instead of modifying the original string in place.
- help(list) displays documentation for the list class and its methods.
Key Takeaways
- A method is defined for a class and called through an object with dotted notation.
- List methods such as append() and sort() modify the original list in place.
- These in-place methods return None, so their mutation effect must be separated from their return value.
- Strings are immutable, so string operations create new strings rather than changing the original string.
- Use help(list) to explore list methods and their documentation.