Concepts / Understanding Object Attributes

Understanding Object Attributes

Lists are objects that bundle together data (the items they contain) and behavior (methods like append(), sort(), and __getitem__()).

  • Programming

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.

list objectdata and behavioritemsstored dataappend()adds an itemsort()rearranges items__getitem__()retrieves an itemcontainsprovidesprovidesprovides
What does a list object contain, and how are its stored items connected to behaviors such as append(), sort(), and __getitem__()?

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]?

  • python

  • java

  • c

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

Code walkthrough
stuff = list()
stuff.append('python')
stuff.append('java')
stuff.append('c')
stuff.sort()
print(stuff)
print(stuff[0])
  1. 1
    After stuff = list(), the list contains no items.
  2. 2
    After the first append(), the list contains python.
  3. 3
    After the next two append() calls, the list contains python, java, and c in that insertion order.
  4. 4
    After sort(), the same list contains c, java, and python in alphabetical order.
  5. 5
    The expression stuff[0] retrieves c because c is now at position 0.
  • The list() call constructs an empty list object.
  • Each append() call adds one string to the end of the same list.
  • sort() rearranges the existing strings alphabetically.
  • stuff[0] retrieves the item at position 0, which is the first item.
Output
['c', 'java', 'python']
c
stuff[]stuff[python, java, c]stuff[c, java, python]append() callssort()
What happens to the same list object after append() adds an item and sort() rearranges its existing items?

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

Definition

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.

python
index 0first positioncstuff[0]javastuff[1]stuff[0]square bracketspythonstuff[2]__getitem__(0)method formselectsretrievesretrieves
How does list[index] correspond to list.__getitem__(index), and which item does the index select?

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.

Best practice

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

  • Common mistake

    Treating append() as if it only reports a change instead of changing the list.

    What it looks like

    Calling stuff.append('python') and continuing to reason as though stuff still has its old contents.

    Why it fails

    append() adds the item to the end of the list and modifies the list in place.

    Do this instead

    Update your traced list state immediately after every append() call.

  • Common mistake

    Forgetting that sort() changes the order of existing items.

    What it looks like

    Assuming the first item remains the first item after stuff.sort().

    Why it fails

    sort() rearranges the items in order.

    Do this instead

    Write down the list before and after sort(), then use the new order when retrieving an item.

  • Common mistake

    Assuming the three retrieval forms select different items.

    What it looks like

    Expecting stuff[0] and stuff.__getitem__(0) to produce different results.

    Why it fails

    The three forms use different syntax but retrieve the same item at position 0.

    Do this instead

    Focus on the index and the list state; the syntax does not change which position is selected.

  • Common mistake

    Thinking of a list as only passive data storage.

    What it looks like

    Describing stuff as only a container while ignoring append(), sort(), and __getitem__().

    Why it fails

    A list object bundles its stored items with behaviors that act on those items.

    Do this instead

    When analyzing a list, identify both its current data and the methods available to operate on it.

Practice the State Trace

Worked example

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]?

  1. 1
    Record insertion order

    After the append() calls, the list contains zebra, apple, and mango in the order they were added.

  2. 2
    Apply sort()

    sort() rearranges the strings alphabetically, producing apple, mango, and zebra.

  3. 3
    Apply index 0

    Index 0 selects the first item in the new order.

Result

stuff[0] retrieves apple.

EASY

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

  1. 1
    A list is an object that bundles stored items with behaviors called methods.
  2. 2
    append() adds an item to the end of a list, while sort() rearranges the list's items; both modify the list in place.
  3. 3
    stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0) retrieve the same item at position 0.
  4. 4
    Square bracket syntax is the standard and most readable way to retrieve a list item.
  5. 5
    Understanding lists as objects prepares you to understand objects that combine data with the actions they can perform.

At a glance

  • 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.

Related concepts

More concepts starting with U