0
0
Pythonprogramming~10 mins

Object lifecycle overview in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object lifecycle overview
Create Object
Initialize __init__
Use Object
Delete Object
Call __del__
This flow shows how an object is created, initialized, used, and then deleted in Python.
Execution Sample
Python
class MyObject:
    def __init__(self):
        print('Object created')
    def __del__(self):
        print('Object deleted')
obj = MyObject()
del obj
This code creates an object, prints a message on creation, then deletes it and prints a message on deletion.
Execution Table
StepActionEvaluationResult
1Create object objCalls __init__Prints 'Object created'
2Object obj readyObject exists in memoryNo output
3Delete object objCalls __del__Prints 'Object deleted'
4Object obj removedNo longer accessibleNo output
💡 Object is deleted and memory is freed, lifecycle ends
Variable Tracker
VariableStartAfter CreationAfter Deletion
objNoneReference to MyObject instanceNo reference (deleted)
Key Moments - 3 Insights
Why do we see 'Object created' printed immediately after creating obj?
Because the __init__ method runs right after the object is created, as shown in execution_table step 1.
Does __del__ run immediately when we call del obj?
Yes, __del__ is called when the object is deleted, as shown in execution_table step 3, printing 'Object deleted'.
What happens if we try to use obj after deletion?
The object no longer exists (execution_table step 4), so using obj will cause an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when the object is created?
A'Object created'
B'Object deleted'
CNothing
D'Object ready'
💡 Hint
See execution_table step 1, the __init__ method prints 'Object created'.
At which step does the object get removed from memory?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Check execution_table step 4, it shows the object is no longer accessible.
If we remove the del obj line, what changes in the execution_table?
AStep 2 would be missing
BStep 1 would not happen
CStep 3 and 4 would not happen
DAll steps remain the same
💡 Hint
Without del obj, __del__ is not called, so steps 3 and 4 do not occur.
Concept Snapshot
Object lifecycle in Python:
1. Creation calls __init__ to initialize.
2. Object is ready to use.
3. Deletion calls __del__ to clean up.
4. Object memory is freed after deletion.
Use del to delete objects explicitly.
Full Transcript
This visual trace shows the lifecycle of a Python object. First, the object is created, triggering the __init__ method which prints 'Object created'. Then the object exists and can be used. When we delete the object using del, the __del__ method runs and prints 'Object deleted'. Finally, the object is removed from memory and cannot be used anymore. Variables track the object's reference before and after creation and deletion. Key moments clarify why __init__ and __del__ run when they do, and what happens if we try to use the object after deletion. The quiz tests understanding of these steps by asking about printed messages and lifecycle stages.