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 "final" class.
✅Solution Using Metaclasses
class NoSubclassingMeta(type):
def __init_subclass__(cls, *args, **kwargs):
raise TypeError(f"Subclassing of class {cls.__name__} is not allowed")
class FinalClass(metaclass=NoSubclassingMeta):
pass
class SubClass(FinalClass): # ❌ Raises TypeError
pass
This technique leverages the __init_subclass__ hook, which is called whenever a class is subclassed. By raising an error here, you effectively make the class "final."
๐งฌ Question 3: Class vs Instance Variables and Method Resolution
class A:
x = 5
def __init__(self):
self.x = 10
def foo(self):
print("A.foo:", self.x)
class B(A):
x = 15
def __init__(self):
super().__init__()
self.x = 20
def foo(self):
print("B.foo:", self.x)
super().foo()
class C(B):
x = 25
def __init__(self):
super().__init__()
self.x = 30
def foo(self):
print("C.foo:", self.x)
super().foo()
obj = C()
obj.foo()
print(obj.x)
print(C.x)
print(B.x)
print(A.x)
๐งพOutput:
C.foo: 30
B.foo: 30
A.foo: 30
30
25
15
5
๐ง Explanation:
self.xrefers to the instance variable, which is set to30inC.__init__.C.x,B.x, andA.xrefer to class variables.- Method resolution follows the MRO (Method Resolution Order), and
super()calls the next method in the hierarchy.
Comments
Post a Comment