Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Classes

A class is a blueprint for your own kind of value: it says once what data every object made from it holds.

Last updated: 30 Sep, 2026 · Python 3.14

A dictionary accepts any key, so a typo adds a new key without any error. A class describes what every ticket has, and each ticket made from it is an object.

__init__, self and making Car objects · from Tutorial 21- Python OOPS Tutorial- Classes, Variables, Methods and Objects · 5:26 to 10:39
The video's error reads __init__() missing 3 required positional arguments; Python 3.10 and later names the class too, Car.__init__(). Its __init__ takes window and stores it as self.windows: the parameter and the attribute are separate names, and they may differ. The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
class Car:
    def __init__(self, window, door, enginetype):
        self.windows = window
        self.doors = door
        self.enginetype = enginetype

car1 = Car(4, 5, "petrol")
car2 = Car(3, 4, "diesel")
print(car1.windows)
print(car2.enginetype)

Each call to Car(...) ran __init__ with the three values, and each car kept its own. The Ticket class below is built the same way, with an id, a customer and a text.

Syntax:

python
class Name:
    def __init__(self, value):
        self.value = value     # an attribute

obj = Name(1)                  # runs __init__

Defining a Ticket class

Example
class Ticket:
    def __init__(self, ticket_id, customer, text):
        self.id = ticket_id
        self.customer = customer
        self.text = text

ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket.customer)
print(ticket.text)

class Ticket: starts the description. By convention class names start with a capital letter.

__init__, with two underscores each side, is the function Python runs when you write Ticket(...). Its first parameter, self, is the new object being made; the lines inside store the arguments on it as attributes, read later with a dot.

You never pass self yourself. Ticket(1, "Asha", ...) gives three arguments, and Python supplies the object as the first.

Making many objects from one class

Example
first = Ticket(1, "Asha", "I was charged twice for one order")
second = Ticket(2, "Ben", "My parcel has not arrived")

second.customer = "Ben T."
print(first.customer, "/", second.customer)

Each object keeps its own attributes. Changing second leaves first alone.

Reading an attribute that is not there

Example
ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket.priority)

A dictionary would have said KeyError; an object says AttributeError. Same idea: you asked for something that was never stored. Delete that line before you add the next example: an error stops the file where it happens.

Printing an object

Example
ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket)

Python does not know which attributes matter, so it prints the class name and a memory address, which changes on every run. dataclasses fixes that.

Dictionary vs class

{"customer": "Asha"}Ticket(1, "Asha", ...)
Read a valueticket["customer"]ticket.customer
FieldsAny key, added any timeThe ones __init__ sets
A missing oneKeyErrorAttributeError

Where classes show up in AI code

  • Frameworks hand you classes to create: a client, an agent, a model.
  • Your own records, such as a ticket or a result, with fixed fields.
Watch out. Forgetting self. inside __init__, as in customer = customer, stores nothing on the object. The value is gone when __init__ ends, and reading ticket.customer later is an AttributeError.
Try it yourself
  • Add a fourth parameter, priority=3, to __init__ and store it. The default keeps the existing calls working.
  • Make a third ticket and print all three customers.
  • Call Ticket(1, "Asha") with one argument missing and read the error.

Every expert started right here.