Concepts / Working with Built-in Objects

Working with Built-in Objects

Dot notation identifies the object and the method together. In stuff.append('item'), stuff identifies the object, append identifies the method belonging to it, and 'item' is the parameter supplied to that method.

  • Programming

From Creation to Cleanup

An object is not just a value sitting in a program. It moves through a lifecycle. First, it is constructed and its internal data is initialized. Next, the program operates on it by calling methods, which may change its internal state. Finally, when the object is no longer needed, it is destroyed and Python calls its __del__ method for cleanup. This three-part pattern gives you a reliable way to read object-oriented code: identify what creates the object, identify what changes it, and identify how it is eventually cleaned up.

What do you think happens?

Suppose a program creates a list, appends an item, reads the first item, and then ends. Which lifecycle phases should you look for?

  • Only construction

  • Construction, operation, and destruction

  • Only operation and destruction

  • Construction and operation, but never destruction

Reveal answer
Answer:

Construction, operation, and destruction

The object is created and initialized, used through a method call and an item lookup, and eventually cleaned up when it is no longer needed.

Construction__init__Operationmethod callsInternal statemodified dataDestruction__del__object is readymethod modifiesobject no longer needed
What happens to an object from creation through initialization, use, state changes, and eventual destruction?

Construction and Initialization

Construction is the first phase of the lifecycle. When you create an object, Python automatically calls the constructor method named __init__. Its role is to set up the object's internal data attributes. This means that initialization happens as part of object creation rather than as a separate method call that you must remember to make later.

python
Notes on the code
  • The list() expression creates the object.
  • During creation, __init__ is automatically invoked to set up the object's internal structure.
  • The name stuff refers to the created object.
Creation expressionlist()List objectnew object__init__set up internal dataReady objectstuffcreatesautomatically invokesinitializes
When and how does __init__ initialize an object's internal data after the object is created?

Methods and Internal State

After construction, the object enters its operation phase. A method is a function that belongs to an object. Methods are called with dot notation, such as stuff.append('item'). Python looks up the append method on stuff, calls it with the supplied parameter, and the method modifies the object's internal state. The important habit is to compare the object's state before and after the call rather than treating the method call as unexplained magic.

Before
stuff = list()
stuff

The list has its initialized internal structure and contains no added item.

After
stuff = list()
stuff.append('item')
stuff

The append method has modified the list's internal state by adding 'item'.

stuffliststufflistEmpty listbefore appenditemafter appendcontainscontains
How does an object's internal data change before and after a method call?
Key point

Dot notation identifies the object and the method together. In stuff.append('item'), stuff identifies the object, append identifies the method belonging to it, and 'item' is the parameter supplied to that method.

Square Brackets and __getitem__

Square bracket syntax is another way to invoke an object's behavior. The expression stuff[0] is shorthand for calling the object's __getitem__ method with 0. The source gives three equivalent forms: stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0). These expressions use different notation, but they represent the same item-access operation.

stuffobject[0]item access__getitem__method0argumentusesshorthand forsupplies
What happens internally when stuff[0] is evaluated, and how does it connect to __getitem__?
Written formUnderlying operationMeaning
stuff[0]__getitem__ with 0Access an item using square bracket syntax
stuff.__getitem__(0)The object's __getitem__ methodCall the method through dot notation
list.__getitem__(stuff, 0)The list type's __getitem__ methodCall the method with the object and index supplied explicitly

The three equivalent forms of item access described in the source material.

python
Notes on the code
  • The first line uses square bracket shorthand.
  • The second line calls __getitem__ through the object.
  • The third line calls the list method with stuff and 0 supplied explicitly.

One Line at a Time

A strong object-oriented reading strategy is to translate each line into the mechanism it invokes. The following compact example is generated to apply the mechanisms described in the source: construction with list(), state change through append, item access through square brackets, and eventual cleanup.

Code walkthrough
stuff = list()
stuff.append('item')
first = stuff[0]
print(first)
  1. 1
    Line 1 creates a list object. During construction, __init__ is automatically called to set up its internal data.
  2. 2
    Line 2 uses dot notation to call the append method belonging to stuff. The method modifies the list's internal state.
  3. 3
    Line 3 uses square bracket syntax. This is shorthand for calling stuff.__getitem__(0).
  4. 4
    Line 4 uses the value obtained from the item-access operation.
  5. 5
    After the object is no longer needed and the program ends, Python calls its __del__ method for cleanup.
Output
item
stuff = list()construction__init__initialize internal datastuff.append('item')method callInternal stateitem addedstuff[0]square bracket syntax__getitem__item accessautomatically invokesreturns ready objectmodifiesshorthand for
Which Python mechanism does each line of object-oriented code invoke, and how does control flow between the line and that mechanism?

Destruction and Cleanup

The final lifecycle phase is destruction. When the object is no longer needed and the program ends, Python automatically calls the destructor method named __del__. For a simple list, cleanup is minimal. For more complex objects that manage files, network connections, or other resources, the destructor is important because it allows those resources to be released properly.

Active objectstuffProgram endsobject no longer needed__del__cleanupuse completesautomatically calls
How does an object move from active use to destruction, and when is __del__ invoked?

Mistakes in Mechanism Tracing

  • Common mistake

    Treating object creation as if it only assigns a name.

    What it looks like
    stuff = list() is read as only creating the name stuff.
    Why it fails

    The line creates an object, and __init__ is automatically called to set up its internal data.

    Do this instead

    Trace both parts: the object is constructed, then __init__ initializes it.

  • Common mistake

    Reading append as a standalone function.

    What it looks like

    stuff.append('item') is described only as append receiving a value.

    Why it fails

    append is a method belonging to stuff, and the method can modify that object's internal state.

    Do this instead

    Identify the object, the method found through dot notation, and the state change.

  • Common mistake

    Assuming square brackets are unrelated to methods.

    What it looks like

    stuff[0] is treated as special syntax with no method behind it.

    Why it fails

    The source identifies stuff[0] as shorthand for stuff.__getitem__(0).

    Do this instead

    Translate square bracket access into the corresponding __getitem__ call.

  • Common mistake

    Stopping the lifecycle trace after the last visible operation.

    What it looks like

    The analysis ends after reading the first item.

    Why it fails

    The object still has a destruction phase. When the program ends, Python calls __del__ for cleanup.

    Do this instead

    After tracing construction and operation, check how the object is cleaned up.

Practice the Trace

MEDIUM

For each line, name the object-oriented mechanism being used. Then describe how the object's state changes, if it changes.

Hints
  • Start by identifying the construction line and the automatic __init__ call.
  • For a dot-notation line, identify the method belonging to the object.
  • For a square-bracket line, write the equivalent __getitem__ form.
  • End your trace with the destruction phase and __del__.
python
Worked example

A complete lifecycle trace

Trace the mechanisms invoked by the generated three-line program.

  1. 1
    Construction

    items = list() creates the list object. The constructor process automatically invokes __init__ to set up its internal data.

  2. 2
    Operation

    items.append('book') calls a method through dot notation. The method modifies the internal state by adding 'book'.

  3. 3
    Item access

    items[0] uses square bracket shorthand for items.__getitem__(0). The resulting item is assigned to selected.

  4. 4
    Destruction

    When the object is no longer needed and the program ends, Python automatically calls __del__ for cleanup.

Result

The trace is construction, initialization, state-changing method call, __getitem__-based access, and eventual destruction.

A Reliable Reading Method

  1. 1
    Find the line that creates the object and identify the automatic __init__ initialization.
  2. 2
    Record the object's important internal data immediately after construction.
  3. 3
    For each dot-notation call, identify the method and decide whether it modifies the object's internal state.
  4. 4
    For each square-bracket expression, rewrite it mentally as a __getitem__ call.
  5. 5
    At the end of the trace, account for the automatic __del__ cleanup phase.

This method turns object-oriented code into a sequence of predictable mechanisms. A line such as stuff.append('item') becomes method lookup followed by a method call and a state change. A line such as stuff[0] becomes a __getitem__ operation. A line such as stuff = list() becomes construction followed by automatic initialization. Once these translations become familiar, the syntax is easier to trace because each visible expression points to an underlying object behavior.

Key Takeaways

  • Objects move through construction, operation, and destruction.
  • The constructor __init__ is called automatically during object creation and sets up internal data attributes.
  • Methods belong to objects, are called with dot notation, and can modify internal state.
  • Square bracket access such as stuff[0] is shorthand for stuff.__getitem__(0); it is equivalent to the two other forms described in the source.
  • When an object is no longer needed at the end of the program, Python calls __del__ for cleanup.

Related concepts

More concepts starting with W