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
Object lifecycle overview
📖 Scenario: Imagine you have a simple program that creates and uses a Car object. You want to understand how this object is created, used, and then removed from memory.
🎯 Goal: You will write a small program that creates a Car object, uses it, and then shows when the object is deleted. This will help you see the full lifecycle of an object in Python.
📋 What You'll Learn
Create a class called Car with an __init__ method
Add a method called drive that prints a message
Create an instance of Car called my_car
Call the drive method on my_car
Delete the my_car object and show a message when it is deleted
💡 Why This Matters
🌍 Real World
Understanding object lifecycle helps in managing resources like memory and files in real programs.
💼 Career
Knowing how objects are created and destroyed is important for writing efficient and bug-free code in software development.
Progress0 / 4 steps
1
Create the Car class with an __init__ method
Create a class called Car with an __init__ method that prints "Car created" when a new object is made.
Python
Hint
The __init__ method runs when you create a new object.
2
Add a drive method to the Car class
Add a method called drive inside the Car class that prints "Car is driving" when called.
Python
Hint
Methods inside a class always have self as the first parameter.
3
Create a Car object and call the drive method
Create an object called my_car from the Car class. Then call the drive method on my_car.
Python
Hint
Use my_car = Car() to create the object and my_car.drive() to call the method.
4
Delete the Car object and show a message
Add a __del__ method to the Car class that prints "Car deleted" when the object is removed. Then delete the my_car object using del my_car.
Python
Hint
The __del__ method runs when the object is deleted with del.
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: