Type Checking & Abstract Classes Interview Questions
isinstance/issubclass, ABC virtual subclasses, namespace packages, and dynamic/lazy imports.
Question 1: What is isinstance()?
Ans
isinstance(obj, Type) checks whether an object is an instance of a type or compatible subclass, and it can accept a tuple of types.
Example
python
value = 10
print(isinstance(value, int))
Important Point
Prefer behavior-based design when practical; type checks are useful when an API genuinely depends on type-specific behavior.
Question 2: What is issubclass()?
Ans
issubclass(A, B) checks whether class A is a subclass of B or B is an appropriate superclass/ABC according to Python's type system.
Example
python
class Dog: pass
class Puppy(Dog): pass
print(issubclass(Puppy, Dog))
Important Point
The first argument must be a class or a TypeError is raised.
Question 3: What is ABC virtual subclass?
Ans
An abstract base class can register an unrelated class as a virtual subclass, allowing certain subclass/instance checks without modifying the registered class's inheritance hierarchy.
Example
python
from abc import ABC
class Printable(ABC): pass
class Report: pass
Printable.register(Report)
print(issubclass(Report, Printable))
Important Point
Registration affects ABC checks; it does not add methods to the registered class.
Question 4: What is a namespace package?
Ans
A namespace package allows portions of one logical package namespace to exist in multiple directories or distributions without a traditional __init__.py in each portion.
Example
text
company/
tools/
services/
Important Point
Namespace packages are useful for separately distributed components but can complicate packaging if used without a clear need.
Question 5: What is importlib?
Ans
importlib exposes Python's import machinery so applications can import modules dynamically and inspect import behavior.
Example
python
import importlib
math = importlib.import_module("math")
print(math.sqrt(16))
Important Point
Dynamic imports should be controlled carefully when module names come from untrusted input.
Question 6: What is lazy import?
Ans
Lazy import delays importing a module or dependency until it is actually needed, which can reduce startup cost in some applications.