script_3.py - List as Stack (LIFO)
Code
See script_3.py for full code.
Explanation
Line 8: class BrowserHistory:
- Defines a class (custom type)
- Classes group related data and functions
Line 9: def __init__(self):
- __init__ is constructor method (called when object created)
- self is reference to instance
- Initializes object state
Line 10: self.history = []
- Instance variable (belongs to this object)
- Empty list to store history stack
- self. prefix makes it accessible across methods
Line 11: self.current = None
- None is Python's null/empty value
- Represents no current page
Line 18: self.history.append(self.current)
- .append() adds to end of list (push operation)
- Stack grows at the end
- LIFO: Last In, First Out
Line 25: previous = self.history.pop()
- .pop() without argument removes and returns last element
- Stack shrinks from the end
- Reverses the append order
- List must not be empty (checked with if self.history)
Line 27: self.current = previous
- Updates current page to popped value
- Simulates going back in browser