Python Essentials for AI Engineer

Course Content

Python Essentials for AI Engineer

6 sections · 48 lessons

What is Object-Oriented Programming (OOP)?


What you need to know

From loose data to objects

Without OOP, state lives in dicts and logic lives in functions that must be handed that dict every time. Nothing stops someone from changing the dict in a way the functions do not expect. A class keeps them together:

Python
class SupportBot:    def __init__(self, model, max_turns=10):        self.model = model              # attributes: the object's data        self.max_turns = max_turns        self.history = []    def ask(self, text):                # method: behaviour on that data        if len(self.history) >= self.max_turns:            raise RuntimeError("conversation too long")        self.history.append(text)        return f"[{self.model}] reply to: {text}"bot = SupportBot("chat-small")print(bot.ask("Where is my refund?"))   # [chat-small] reply to: Where is my refund?print(len(bot.history))                 # 1

The four pillars, in Python terms

PillarIdeaHow Python does it
Encapsulationkeep state behind methods that protect it_name convention, @property
Inheritancea child class reuses and specialises a parentclass Child(Parent):, super()
Polymorphismone method call, many behavioursduck typing, overriding, dunder methods
Abstractionshow what, hide howsmall public APIs, abc.ABC

Everything in Python is an object

Numbers, strings, functions and classes are all objects with a type and methods: (255).bit_length() returns 8 and "upi".upper() returns "UPI". So even "non-OOP" Python code uses objects all the time.

When not to use a class

A class with an __init__ and one method is usually just a function in disguise. If there is no state to protect and no family of variants, a function is simpler to read and test.

A real-life example

Why does every scikit-learn model — logistic regression, random forest, SVM — have the same fit(X, y) and predict(X) methods? Because the library is designed around OOP. Each estimator is an object that stores its learned parameters as attributes, and all of them share one interface. So one Pipeline, one cross_val_score and one grid search work with more than a hundred model classes.

You can use the same idea in your own team's code:

Python
class KeywordClassifier:    def predict(self, text):        return "refund" if "refund" in text.lower() else "other"class AlwaysOther:    def predict(self, text):        return "other"def accuracy(model, data):    return sum(model.predict(t) == y for t, y in data) / len(data)data = [("Refund not received", "refund"), ("Change my address", "other"), ("REFUND pls", "refund")]for model in (KeywordClassifier(), AlwaysOther()):    print(type(model).__name__, round(accuracy(model, data), 2))# KeywordClassifier 1.0# AlwaysOther 0.33

When an LLM-based classifier arrives next month, it only needs a predict method to plug into the same accuracy function.

Follow-up questions to expect

  • "How is OOP different from procedural programming?" — Procedural code passes data through a sequence of functions; OOP keeps data and the functions that change it together inside objects.
  • "Is everything in Python an object?" — Yes. Integers, functions, modules and classes themselves all have a type and attributes.
  • "When would you avoid a class?" — When there is no state to manage — a pure transformation like clean_text(text) is better as a function.