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.
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?
Reveal 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 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.
- 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.
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.
stuff = list()
stuffThe list has its initialized internal structure and contains no added item.
stuff = list()
stuff.append('item')
stuffThe append method has modified the list's internal state by adding 'item'.
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.
| Written form | Underlying operation | Meaning |
|---|---|---|
| stuff[0] | __getitem__ with 0 | Access an item using square bracket syntax |
| stuff.__getitem__(0) | The object's __getitem__ method | Call the method through dot notation |
| list.__getitem__(stuff, 0) | The list type's __getitem__ method | Call the method with the object and index supplied explicitly |
The three equivalent forms of item access described in the source material.
- 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.
stuff = list()
stuff.append('item')
first = stuff[0]
print(first)- 1Line 1 creates a list object. During construction,
__init__is automatically called to set up its internal data. - 2Line 2 uses dot notation to call the append method belonging to stuff. The method modifies the list's internal state.
- 3Line 3 uses square bracket syntax. This is shorthand for calling
stuff.__getitem__(0). - 4Line 4 uses the value obtained from the item-access operation.
- 5After the object is no longer needed and the program ends, Python calls its
__del__method for cleanup.
itemDestruction 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.
Mistakes in Mechanism Tracing
Common mistake Treating object creation as if it only assigns a name.
What it looks likestuff = list() is read as only creating the name stuff.Why it failsThe line creates an object, and
__init__is automatically called to set up its internal data.Do this insteadTrace both parts: the object is constructed, then __init__ initializes it.
Common mistake Reading append as a standalone function.
What it looks likestuff.append('item') is described only as append receiving a value.
Why it failsappend is a method belonging to stuff, and the method can modify that object's internal state.
Do this insteadIdentify the object, the method found through dot notation, and the state change.
Common mistake Assuming square brackets are unrelated to methods.
What it looks likestuff[0] is treated as special syntax with no method behind it.
Why it failsThe source identifies stuff[0] as shorthand for
stuff.__getitem__(0).Do this insteadTranslate square bracket access into the corresponding __getitem__ call.
Common mistake Stopping the lifecycle trace after the last visible operation.
What it looks likeThe analysis ends after reading the first item.
Why it failsThe object still has a destruction phase. When the program ends, Python calls
__del__for cleanup.Do this insteadAfter tracing construction and operation, check how the object is cleaned up.
Practice the Trace
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__.
A complete lifecycle trace
Trace the mechanisms invoked by the generated three-line program.
- 1Construction
items =
list()creates the list object. The constructor process automatically invokes__init__to set up its internal data. - 2Operation
items.append('book')calls a method through dot notation. The method modifies the internal state by adding 'book'. - 3Item access
items[0] uses square bracket shorthand for
items.__getitem__(0). The resulting item is assigned to selected. - 4Destruction
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
- 1Find the line that creates the object and identify the automatic
__init__initialization. - 2Record the object's important internal data immediately after construction.
- 3For each dot-notation call, identify the method and decide whether it modifies the object's internal state.
- 4For each square-bracket expression, rewrite it mentally as a
__getitem__call. - 5At 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.