Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the object lifecycle in Python?
The object lifecycle is the process an object goes through from creation, usage, and finally to destruction when it is no longer needed.
Click to reveal answer
beginner
How is an object created in Python?
An object is created when a class is called like a function, which runs the <code>__new__</code> method to create the object and then the <code>__init__</code> method to initialize it.
Click to reveal answer
beginner
What happens during the usage phase of an object?
During usage, the object’s methods and properties are accessed and modified as needed by the program.
Click to reveal answer
intermediate
How does Python destroy an object?
Python destroys an object when its reference count drops to zero, triggering the __del__ method and freeing memory.
Click to reveal answer
intermediate
What is the role of __del__ method in the object lifecycle?
The __del__ method is called when an object is about to be destroyed, allowing cleanup actions before memory is freed.
Click to reveal answer
Which method is called when a new object is created in Python?
A__init__
B__del__
C__str__
D__call__
✗ Incorrect
The __init__ method initializes a new object when it is created.
When does Python destroy an object?
AImmediately after creation
BWhen its reference count reaches zero
CWhen the program starts
DWhen __init__ is called
✗ Incorrect
Python destroys objects when no references to them remain.
What is the purpose of the __del__ method?
ATo clean up before the object is destroyed
BTo initialize the object
CTo print the object
DTo copy the object
✗ Incorrect
The __del__ method allows cleanup actions before the object is removed.
Which phase comes after object creation in the lifecycle?
ADestruction
BInitialization
CGarbage collection
DUsage
✗ Incorrect
After creation, the object is used by the program.
What triggers the object destruction process?
ACalling __init__
BCalling __str__
CReference count dropping to zero
DAssigning a new value
✗ Incorrect
When no references remain, Python destroys the object.
Describe the main stages of an object's lifecycle in Python.
Think about what happens from when you make an object to when it disappears.
You got /5 concepts.
Explain how Python knows when to destroy an object.
Consider how Python tracks if an object is still needed.
You got /4 concepts.
Practice
(1/5)
1. What is the first step in the lifecycle of a Python object?
easy
A. Creation of the object in memory
B. Deletion of the object
C. Garbage collection
D. Assignment of a variable
Solution
Step 1: Understand object lifecycle start
The lifecycle of a Python object begins when it is created in memory.
Step 2: Differentiate from other lifecycle stages
Deletion and garbage collection happen later, after creation and use.
Final Answer:
Creation of the object in memory -> Option A
Quick Check:
Object lifecycle starts with creation [OK]
Hint: Object lifecycle always starts with creation [OK]
Common Mistakes:
Confusing creation with deletion
Thinking garbage collection happens first
Assuming variable assignment is the first step
2. Which of the following is the correct syntax to define a destructor method in a Python class?
easy
A. def destructor(self):
B. def __del__(self):
C. def delete(self):
D. def __destroy__(self):
Solution
Step 1: Recall destructor method name
In Python, the destructor method is named __del__ with double underscores before and after.
Step 2: Check syntax correctness
The correct syntax is def __del__(self): which matches Python's special method naming.
Final Answer:
def __del__(self): -> Option B
Quick Check:
Destructor method = __del__ [OK]
Hint: Destructor method is always named __del__ [OK]
Common Mistakes:
Using wrong method names like destructor or delete
Missing double underscores
Confusing with constructor __init__
3. What will be the output of the following code?
class MyClass:
def __init__(self):
print('Created')
def __del__(self):
print('Deleted')
obj = MyClass()
print('Object in use')
del obj
print('End')
medium
A. Created\nObject in use\nDeleted\nEnd
B. Created\nDeleted\nObject in use\nEnd
C. Object in use\nCreated\nDeleted\nEnd
D. Created\nObject in use\nEnd
Solution
Step 1: Trace object creation and constructor call
When obj = MyClass() runs, __init__ prints 'Created'.
Step 2: Follow print and deletion order
Next, 'Object in use' prints. Then del obj calls __del__, printing 'Deleted'. Finally, 'End' prints.
Final Answer:
Created\nObject in use\nDeleted\nEnd -> Option A
Quick Check:
Constructor then prints, then destructor after del [OK]
Hint: Destructor runs only after del or object goes out of scope [OK]
Common Mistakes:
Assuming destructor runs immediately after creation
Ignoring order of print statements
Thinking del obj skips destructor
4. Identify the error in this code related to object lifecycle: