Hitesh Garg’s Post

One of my favorite interview questions is simple on the surface: - Why do we use context managers in Python? Almost instantly, the answer comes: So we don’t have to close connections manually. It auto-closes them. Fair. But then I take it one step further: Why do we need to close connections at all? Why not just leave them open? That’s where things get interesting. Some candidates pause. Some say: It’s not a good practice to leave things open. And I push again: But why is it not a good practice? Because there has to be a reason… right? Only a handful go deeper and talk about: - Limited system resources - File descriptors - Connection/socket limits managed by the OS And that’s the moment you can clearly see the difference. 💡 Context managers (`with` statement) are not just about cleaner syntax. They ensure deterministic resource management, releasing critical resources like files, sockets, and locks as soon as they’re no longer needed. Sometimes, it’s not about what you know… but how far you can go when someone asks “why?” Follow Hitesh Garg for more 🫡 #python #interviews #backend

I would have started with why to use python 🤣

Like
Reply

To view or add a comment, sign in

Explore content categories