Tuples and Immutability
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 in place, while a string or tuple is immutable. That difference affects both the original object and any other variable referring to it. It also affects what an operation returns. The key question is not only “What value does this operation produce?” but also “Did the original object change?”
What do you think happens?
Suppose two variables refer to the same list. If the list is changed through one variable, what will the other variable show?
Reveal answer
Answer: The other variable shows the changed list.
Both variables refer to the same list object. An in-place list method changes that object, so every reference to it observes the change.
Following a List Through an In-Place Change
numbers = [1, 2, 3] result = numbers.append(4) numbers result
An in-place method changes the original list object. It does not produce a new list that must be assigned back to the variable. Methods such as append() and sort() return None after making their changes.
Methods and Dotted Notation
A method is a function defined specifically for a class. When you have a list object, you access its methods with dotted notation: the object comes first, followed by a dot and the method name. In mylist.append(item), append is associated with the list class and operates in the context of mylist. A standalone function is not tied to a particular object in this way.
The expression my_list.sort() uses dotted notation because sort is a method available for list objects. The method changes my_list directly. This is different from imagining sort as a general operation that returns a separate list.
Lists, Strings, and Tuples
Mutable objects can be modified in place. Lists are mutable, so methods such as append() and sort() change the original list. Immutable objects cannot be changed after they have been created. Strings and tuples are immutable types in this discussion, so an operation on them creates a new object rather than changing the original object in place.
| Type | Can the original object change in place? | What should you expect from an operation? |
|---|---|---|
| List | Yes | The original list can be modified directly; in-place methods return None. |
| String | No | An operation creates a new string rather than changing the original string. |
| Tuple | No | The original tuple cannot be changed; an operation must produce a different object when a changed value is needed. |
Shared References
Two Names for One List
Consider two variables that refer to the same list. What happens after the list is modified through one variable?
Create the list: A list contains [1, 2, 3], and one variable refers to it.
Create another reference: A second variable is assigned to the first variable, so both variables refer to the same list object.
Modify through one reference: Calling append(4) changes the shared list in place.
Inspect both references: Both variables show [1, 2, 3, 4] because both still refer to the changed list.
An in-place modification is visible through every variable that refers to the same list.
[1, 2, 3, 4]This same issue can appear when a list is passed to a function. If the function modifies the list, the original list in the calling code is also modified. That behavior follows from the shared reference to the mutable list object.
Finding List Methods
When you do not remember which operations a list supports, use help(list). The help() function displays documentation for the list class, including the methods available for list objects and information about what those methods do.
Treat help(list) as a discovery tool. It lets you inspect the list class instead of guessing method names or assuming that a list operation behaves like a string operation.
Mistakes with Mutability
Assuming append() returns the changed list
append() changes numbers in place and returns None.
Fix:
Call numbers.append(4), then use numbers as the changed list.Assuming a second variable holds an independent copy
Both variables refer to the same list object.
Fix:
Remember that changing the list through either variable affects the shared object.Treating a list method like a standalone function
The list operation is accessed as a method through dotted notation.
Fix:
Use my_list.append(4).Expecting strings and tuples to change in place like lists
Strings and tuples are immutable types.
Fix:
Expect a new object when a changed string or tuple value is needed.
Check Your Understanding
Explain what happens in this sequence: a list is assigned to first, second is assigned to first, and then second.append(9) is called. State what first refers to afterward and what value append returns.
Hints
- Ask whether first and second refer to one list or two separate lists.
- Recall that append changes a list in place.
- Recall the return value of an in-place list method.
Use help(list) to inspect the list class. Find a method that changes a list in place and describe whether it changes the original list, returns a new list, or returns None.
Hints
- Begin by evaluating help(list).
- Look for the method descriptions in the list documentation.
- Compare the method's effect with the behavior of append() or sort().
Working Rules
- Methods belong to a class and are called with dotted notation on an object of that class.
- Lists are mutable, so methods such as append() and sort() can modify the original list in place.
- In-place list methods change the list and return None rather than returning a new list.
- If multiple variables refer to the same list, a change through one variable is visible through all of them.
- Strings and tuples are immutable, so operations do not modify the original object in place.
- Use help(list) to inspect the methods available for list objects.
Key Takeaways
- A method is defined for a class and is accessed through an object with dotted notation.
- A mutable list can be changed in place, while immutable strings and tuples cannot be changed in place.
- List methods such as append() and sort() modify the original list and return None.
- Multiple variables can refer to the same list, so an in-place change is visible through every reference.
- help(list) displays documentation for the list class and its available methods.