Understanding Object Attributes
Lists are objects that bundle together data (the items they contain) and behavior (methods like append(), sort(), and __getitem__()).
A List Is More Than a Container
When you create a list, you are creating an object of type list. That object combines data, meaning the items it contains, with behavior, meaning the actions it can perform. The items are the list's stored data; methods such as append(), sort(), and __getitem__() are behaviors available through the list object.
What do you think happens?
Suppose a list receives the strings python, java, and c through append() calls and then sort() is called. What item will be retrieved by stuff[0]?
Reveal answer
Answer: c
append() adds each new item to the end. sort() then arranges the strings alphabetically, so c becomes the first item.
Following the List State
stuff = list() stuff.append('python') stuff.append('java') stuff.append('c') stuff.sort() print(stuff) print(stuff[0])
The important detail is that append() and sort() modify the list in place. The list first gains another item, and then its existing items are rearranged. These operations do not describe a separate result that replaces the list in this example; they change the list object you are already using. This ability to change after creation is why lists are described as mutable.
Methods as Object Behaviors
A method is a behavior or action that an object can perform. When you write stuff.append('python'), you invoke append() on the list object named stuff. When you write stuff.sort(), you invoke sort() on that same object.
The object-oriented perspective helps explain why list operations feel connected. A list is not only passive storage. It is an active object that knows how to add items to itself, sort its items, retrieve an item, and perform other list operations. The data and the actions that work with that data are bundled together.
Three Ways to Retrieve an Item
Python lets you express retrieval of the first item in a list in three equivalent ways: stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0). Each form retrieves the item at position 0. The square bracket form is the standard and most convenient syntax.
The double underscores in __getitem__ indicate a special method that Python uses internally when square bracket syntax is used. Calling stuff.__getitem__(0) makes the method call explicit on the list object. Calling list.__getitem__(stuff, 0) is even more explicit: it calls the method from the list class and passes stuff as the first parameter. Although these forms show different levels of detail, they retrieve the same item.
Use square bracket syntax for ordinary list item retrieval. The direct method forms are useful for understanding the relationship between Python syntax and object methods, but square brackets are the standard and most readable form.
Mistakes with List Objects
Treating append() as if it only reports a change instead of changing the list.
append() adds the item to the end of the list and modifies the list in place.
Fix:
Update your traced list state immediately after every append() call.Forgetting that sort() changes the order of existing items.
sort() rearranges the items in order.
Fix:
Write down the list before and after sort(), then use the new order when retrieving an item.Assuming the three retrieval forms select different items.
The three forms use different syntax but retrieve the same item at position 0.
Fix:
Focus on the index and the list state; the syntax does not change which position is selected.Thinking of a list as only passive data storage.
A list object bundles its stored items with behaviors that act on those items.
Fix:
When analyzing a list, identify both its current data and the methods available to operate on it.
Practice the State Trace
Tracing Retrieval After Sorting
A list receives the strings zebra, apple, and mango using append(), then sort() is called. Which item is retrieved by stuff[0]?
Record insertion order: After the append() calls, the list contains zebra, apple, and mango in the order they were added.
Apply sort(): sort() rearranges the strings alphabetically, producing apple, mango, and zebra.
Apply index 0: Index 0 selects the first item in the new order.
stuff[0] retrieves apple.
Create a state trace for a list that starts empty, receives the items 8, 3, and 5 through append() calls, and then receives a sort() call. Write the list after each change and identify the item returned by stuff[0].
Hints
- Record the list after each append() call.
- sort() arranges numbers numerically.
- Index 0 means the first item after sorting.
What to Remember
- A list is an object that bundles stored items with behaviors called methods.
- append() adds an item to the end of a list, while sort() rearranges the list's items; both modify the list in place.
- stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0) retrieve the same item at position 0.
- Square bracket syntax is the standard and most readable way to retrieve a list item.
- Understanding lists as objects prepares you to understand objects that combine data with the actions they can perform.
Key Takeaways
- Lists combine data, in the form of stored items, with behavior, in the form of methods.
- append() adds to the end of a list and sort() rearranges its items in place.
- The expressions stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0) are equivalent ways to retrieve the item at position 0.
- Tracing the list after every method call makes object state changes visible.
- Lists provide a concrete foundation for understanding object-oriented programming.