Python OOP and software design
Understand classes, object behavior and maintainable AI pipeline code.
Use small composable classes for preprocessing, inference and evaluation.
Interview questions
Choose a question to reveal its answer.
01EasyCoreWhat is the difference between a class and an instance?
Answer
What is the difference between a class and an instance?
A class defines a type and its behavior; an instance is a particular object of that type with its own state.
Instantiate two preprocessing objects with different configurations.
from dataclasses import dataclass
@dataclass
class ModelConfig:
threshold: float = 0.5
def classify(self, probability):
return int(probability >= self.threshold)
print(ModelConfig().classify(0.8))Instance configuration is separate from class behavior.
02EasyCoreHow do instance variables differ from class variables?
Answer
How do instance variables differ from class variables?
Instance attributes normally belong to individual objects; class attributes are shared through the class unless shadowed.
Keep a fitted scaler's learned mean on the instance rather than a mutable class attribute.
03EasyCoreWhat is the purpose of __init__ in Python?
Answer
What is the purpose of __init__ in Python?
It initializes an already-created instance. Object creation is handled by __new__.
Store model configuration when creating an inference service.
04MediumCoreWhen is inheritance useful, and when should you prefer composition?
Answer
When is inheritance useful, and when should you prefer composition?
Inheritance expresses a subtype relationship. Composition delegates work to contained objects and can reduce coupling.
Compose a pipeline from a tokenizer, retriever and generator.
05MediumHigh priorityHow does Python support polymorphism?
Answer
How does Python support polymorphism?
Different objects can expose a compatible interface, often through duck typing, and be used interchangeably.
Call predict on compatible sklearn estimators.
06HardHigh priorityHow does Python resolve methods in multiple inheritance?
Answer
How does Python resolve methods in multiple inheritance?
Python uses C3 linearization to compute a consistent class ordering; super follows that ordering.
Inspect Class.__mro__ when a method is inherited from multiple parents.
07MediumHigh priorityWhat is the difference between staticmethod and classmethod?
Answer
What is the difference between staticmethod and classmethod?
A static method receives no implicit instance or class argument; a class method receives the class as its first argument.
Use a classmethod as an alternative constructor and a staticmethod for a stateless helper.
08MediumWhy use @property instead of exposing an attribute directly?
Answer
Why use @property instead of exposing an attribute directly?
A property exposes managed attribute access through methods while preserving normal access syntax.
Validate an inference threshold before allowing it to change.
09MediumHow can an abstract base class enforce an interface?
Answer
How can an abstract base class enforce an interface?
Abstract methods declare operations subclasses must implement before instantiation, unless a concrete implementation is available.
Require every retriever implementation to provide a search method.
10MediumWhen should you use a dataclass?
Answer
When should you use a dataclass?
A dataclass generates methods such as initialization and representation from declared fields; optional flags affect equality and mutability.
Represent typed experiment settings with less boilerplate.
Priority and difficulty are editorial study recommendations.
Technical reference: Documentation / paper ↗
Try a few practice questions
Explore the questions before creating a free account.