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__() 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.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)4 diesel
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:
class Name:
def __init__(self, value):
self.value = value # an attribute
obj = Name(1) # runs __init__Defining a Ticket class
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)Asha I was charged twice for one order
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
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)Asha / Ben T.
Each object keeps its own attributes. Changing second leaves first alone.
Reading an attribute that is not there
ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket.priority)Traceback (most recent call last):
File "main.py", line 2, in <module>
print(ticket.priority)
AttributeError: 'Ticket' object has no attribute '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
ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket)<__main__.Ticket object at 0x10a240d70>
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 value | ticket["customer"] | ticket.customer |
| Fields | Any key, added any time | The ones __init__ sets |
| A missing one | KeyError | AttributeError |
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.
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.Related
- Previous: try and except
- Next: Methods
- Reference: Classes in the Python tutorial
- 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.