19. Object-Oriented Python

Chapter 19 of 36 · 15 min

Object-oriented programming (OOP) in Python isn't about academic hierarchies—it's about organizing code that manages state and behavior. For AI tools, you constantly deal with models, configurations, and data loaders that benefit from encapsulation.

The basic pattern: group related data and functions into classes. A class is a blueprint; instances are actual objects with their own state.

class DataPreprocessor:
    def __init__(self, normalize=True, missing_strategy="mean"):
        self.normalize = normalize
        self.missing_strategy = missing_strategy
        self._fitted = False
    
    def fit(self, data):
        """Compute statistics needed for transformation."""
        self._mean = data.mean()
        self._std = data.std()
        self._fitted = True
        return self
    
    def transform(self, data):
        if not self._fitted:
            raise ValueError("Call fit() before transform()")
        if self.normalize:
            return (data - self._mean) / self._std
        return data
    
    def fit_transform(self, data):
        return self.fit(data).transform(data)

# Usage
preprocessor = DataPreprocessor(normalize=True)
cleaned_data = preprocessor.fit_transform(raw_data)

A few things to notice: self is how instance methods access their own data. The underscore prefix on _fitted, _mean, and _std signals these are internal details—not part of the public API. Python conventions let you express intent without syntax.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a MovingAverage class that accepts a window_size in __init__. Add a consume(value) method that updates an internal list and returns the current moving average. Add a reset() method. Instantiate it and process this list: [1, 2, 3, 4, 5] and print each average.