Advanced Python Interview Questions (4+ Years)
1. What is the difference between deepcopy() and copy()?
copy() creates a shallow copy, whereas deepcopy() creates a deep copy. Shallow copy copies references of
nested objects; deep copy duplicates them.
Example:
import copy
lst = [[1, 2], [3, 4]]
shallow = copy.copy(lst)
deep = copy.deepcopy(lst)
lst[0][0] = 100
# shallow reflects the change, deep does not.
2. How does Python?s garbage collector handle circular references?
Python uses reference counting and a generational garbage collector to collect cyclic references. The gc
module can detect and collect circular references.
3. How can you make a custom object hashable and sortable?
Implement __hash__ and __eq__ for hashable objects, and __lt__ or other comparison methods for sorting.
Example:
class Person:
def __init__(self, name): self.name = name
def __hash__(self): return hash(self.name)
def __eq__(self, other): return self.name == other.name
def __lt__(self, other): return self.name < other.name
4. Difference between staticmethod, classmethod, and instance methods?
- instance method: first argument is self
- classmethod: first argument is cls
- staticmethod: no implicit arguments
5. Why doesn?t Python?s multithreading achieve true parallelism?
Advanced Python Interview Questions (4+ Years)
Because of the Global Interpreter Lock (GIL), only one thread executes Python bytecode at a time. For true
parallelism, use multiprocessing.
6. How do generators work and why are they memory efficient?
Generators yield values one by one using the 'yield' keyword. They don?t store all values in memory, making
them suitable for large data streams.
7. What are metaclasses in Python?
A metaclass is a class of a class. It controls the creation and behavior of classes. Example use case: ORM
field validation in Django.
8. How does async/await work in Python?
They enable asynchronous programming. Python uses an event loop to run async functions. Libraries like
asyncio are built on top of it.
9. What is a context manager and how do you create one?
Context managers manage resources. You can create one using a class with __enter__ and __exit__
methods or using @contextlib.contextmanager.
10. Explain the difference between is and ==.
'is' checks for identity (same object in memory), '==' checks for equality in value.
11. How do you create a plugin system in Python?
Use dynamic imports (importlib), entry points, or abstract base classes. Useful for extensible frameworks.
12. What is the difference between @property and a regular method?
@property allows you to access a method like an attribute. It?s used for controlled attribute access.
13. What are *args and **kwargs used for?
They allow passing variable number of arguments to a function. *args for non-keyworded, **kwargs for
keyworded arguments.
14. How do you prevent race conditions in Python?
Advanced Python Interview Questions (4+ Years)
Use thread synchronization methods like threading.Lock or use multiprocessing for CPU-bound tasks.
15. What is the difference between mutable and immutable types in Python?
Mutable objects can be changed in-place (e.g., list, dict); immutable cannot (e.g., int, str, tuple). Tuples can
contain mutable elements.