Advanced Python Interview Questions with Code
Examples
What is a decorator?
A decorator modifies the behavior of a function without changing its code.
def my_decorator(func): def wrapper(): print("Before") func()
print("After") return wrapper @my_decorator def say_hi(): print("Hi")
What is a generator?
A generator yields values one at a time using yield.
def count_up(n): for i in range(n): yield i
What is GIL?
Global Interpreter Lock ensures only one thread executes Python bytecode at a time.
What is multithreading vs multiprocessing?
Threads share memory, processes have separate memory.
from multiprocessing import Process def task(): print("Running") p =
Process(target=task) [Link]()
What is context manager?
Manages resources automatically.
with open("[Link]", "w") as f: [Link]("Hello")
What is shallow vs deep copy?
Deep copy duplicates nested objects.
import copy a = [[1,2]] b = [Link](a)