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.
Jump into concepts and practice - no test required
class MyObject: def __init__(self): print('Object created') def __del__(self): print('Object deleted') obj = MyObject() del obj
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Create object obj | Calls __init__ | Prints 'Object created' |
| 2 | Object obj ready | Object exists in memory | No output |
| 3 | Delete object obj | Calls __del__ | Prints 'Object deleted' |
| 4 | Object obj removed | No longer accessible | No output |
| Variable | Start | After Creation | After Deletion |
|---|---|---|---|
| obj | None | Reference to MyObject instance | No reference (deleted) |
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.
class MyClass:
def __init__(self):
print('Created')
def __del__(self):
print('Deleted')
obj = MyClass()
print('Object in use')
del obj
print('End')class Sample:
def __init__(self):
print('Init called')
def __del__(self):
print('Del called')
obj = Sample()
obj = None
print('Done')class Item:
def __init__(self, name):
self.name = name
print(f'Created {name}')
def __del__(self):
print(f'Deleted {self.name}')
def create_items():
items = [Item('a'), Item('b'), Item('c')]
print('Items created')
create_items()
print('Function ended')