Exercise 1: Library vs. Framework — Two Concrete Examples — Possible Solution ==================================================================== LIBRARY EXAMPLE: PYTHON'S OWN requests ------------------------------ import requests response = requests.get('https://example.com/api/data') print(response.status_code) This code decides, entirely on its own, exactly when the GET request fires: whenever this specific line of code executes. Nothing about requests itself decides when to run - it sits idle until your own code chooses to call requests.get(). Control flow belongs entirely to the calling application. If this line is never reached (say, an earlier if-statement skips it), requests never does anything at all. FRAMEWORK EXAMPLE: A DJANGO VIEW FUNCTION ------------------------------ def product_detail(request, product_id): product = Product.objects.get(pk=product_id) return render(request, 'product_detail.html', {'product': product}) This function is never called directly by any of your own code anywhere in the application. It's registered once, in urls.py, as the handler for a specific URL pattern. Django's own request-handling machinery is what actually calls product_detail - and only at the one moment a real HTTP request arrives whose URL matches the registered pattern. The developer writes the function's own body, but Django decides exactly when (and whether, and with what arguments) it ever actually runs. THE REAL DISTINCTION, STATED PRECISELY ------------------------------ In the requests example, the call originates inside application code and reaches into the library. In the product_detail example, the call originates inside the framework's own internal machinery and reaches into application code. This is exactly the "who calls whom" reversal this chapter names as inversion of control - the same underlying mechanism Martin Fowler's own 2004 article set out to clarify, and the same one informally nicknamed the Hollywood Principle: "don't call us, we'll call you." WHY THIS WORKS AS AN ANSWER ------------------------------ It picks one real example of each kind, traces precisely which piece of code initiates the actual call in each case, and explicitly names which direction control flows - confirming the "who calls whom" test is genuinely applicable to real code, not just an abstract definition.