Python Tricky Questions
Exploring Python Python is known for its simplicity and readability, but beneath its clean syntax lie some subtle behaviors that can trip up even seasoned developers. In this post, I’ll walk you through a few tricky Python interview questions I’ve encountered—both as a candidate and an interviewer. These questions test not just syntax knowledge, but a deep understanding of how Python works under the hood. 🔁 Question 1: The Mutable Default Argument Trap def func(a, L=[]): L.append(a) return L print(func(1)) print(func(2)) print(func(3)) ❓What’s the Output? [1] [1, 2] [1, 2, 3] 💡Why? In Python, default arguments are evaluated once at function definition time. So the list L is shared across all calls to func unless explicitly overridden. This is a common pitfall. ✅How to Fix It def func(a, L=None): if L is None: L = [] L.append(a) return L 🚫 Question 2: Preventing Subclassing in Python Sometimes, you want to create a class that cannot be subclassed —a ...