Concepts / Working with Built-in Objects

Working with Built-in Objects

Objects follow a predictable lifecycle: construction (via __init__), operation (via method calls that modify state), and destruction (via __del__).

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

object is readymethod modifiesobject no longer neededConstruction__init__Operationmethod callsInternal statemodified dataDestruction__del__
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
createsautomatically invokesinitializesCreation expressionlist()List objectnew object__init__set up internal dataReady objectstuff
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.

containscontainsstuffliststufflistEmpty listbefore appenditemafter append
How does an object's internal data change before and after a method call?

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.

usesshorthand forsuppliesstuffobject[0]item access__getitem__method0argument
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

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.

stuff = list() stuff.append('item') first = stuff[0] print(first)

Output
item
automatically invokesreturns ready objectmodifiesshorthand forstuff = list()construction__init__initialize internal datastuff.append('item')method callInternal stateitem addedstuff[0]square bracket syntax__getitem__item access
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.

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

Mistakes in Mechanism Tracing

  • Treating object creation as if it only assigns a name.

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

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

  • Reading append as a standalone function.

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

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

  • Assuming square brackets are unrelated to methods.

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

    Fix: Translate square bracket access into the corresponding __getitem__ call.

  • Stopping the lifecycle trace after the last visible operation.

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

    Fix: 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

A complete lifecycle trace

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

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

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

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

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

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

A Reliable Reading Method

  1. Find the line that creates the object and identify the automatic __init__ initialization.
  2. Record the object's important internal data immediately after construction.
  3. For each dot-notation call, identify the method and decide whether it modifies the object's internal state.
  4. For each square-bracket expression, rewrite it mentally as a __getitem__ call.
  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.