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 →

Methods

A method is a function defined inside a class, and it works on the object it is called on through self.

Last updated: 30 Sep, 2026 · Python 3.14

The Ticket class from Classes holds data but does nothing. Methods give it actions.

Adding a method to the Car class · from Tutorial 21- Python OOPS Tutorial- Classes, Variables, Methods and Objects · 11:15 to 13:15
The video returns its message with .format(); the run below uses an f-string. The method's name, self_driving, has nothing to do with the self parameter; any name works.
ExampleFrom the video, run here
class Car:
    def __init__(self, window, door, enginetype):
        self.windows = window
        self.doors = door
        self.enginetype = enginetype

    def self_driving(self):
        return f"This is a {self.enginetype} car"

car1 = Car(4, 5, "petrol")
print(car1.self_driving)
print(car1.self_driving())

Without brackets, car1.self_driving is the method itself, printed as a bound method with a memory address. With brackets it runs, and self.enginetype reads this car's engine type. The Ticket class below gets a method the same way.

Syntax:

python
class Name:
    def action(self):
        ...            # self is the object the method was called on

obj.action()

Adding a categorise method

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

    def categorise(self):
        text = self.text.lower()
        if "charged" in text or "refund" in text:
            self.category = "billing"
        elif "parcel" in text or "arrived" in text:
            self.category = "shipping"
        else:
            self.category = "other"

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

__init__ now starts every ticket with category set to None. categorise is a method: def inside the class, with self first.

ticket.categorise() passes ticket in as self, so self.text is this ticket's text and setting self.category changes this ticket. It is the categorise function from Functions, now keeping its answer on the object.

Adding a method that returns

Exampleadded inside class Ticket, below categorise
    def summary(self):
        return f"#{self.id} {self.customer}: {self.category}"
Example
tickets = [
    Ticket(1, "Asha", "I was charged twice for one order"),
    Ticket(2, "Ben", "My parcel has not arrived"),
]
for ticket in tickets:
    ticket.categorise()
    print(ticket.summary())

A list of objects works like the list of dictionaries in Lists of dictionaries, and each one knows how to sort and describe itself.

Forgetting self

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

    def shout():
        return "TICKET"

Ticket("refund").shout()

Python always passes the object as the first argument to a method. A method written without a parameter for it receives one argument it has no room for.

A function vs a method

FunctionMethod
DefinedAt the top level of a fileInside a class
Called ascategorise(text)ticket.categorise()
Gets its data fromIts argumentsself, plus any arguments

Where methods show up in AI code

  • Most framework calls are methods: client.chat.completions.create(...), agent.invoke(...).
  • Keeping the steps that belong to one record, such as a ticket, next to its data.
Watch out. ticket.summary without brackets does not run the method. It prints something like <bound method Ticket.summary ...>; add () to call it.
Try it yourself
  • Add a method is_urgent(self) that returns True for billing tickets.
  • Call ticket.summary() before ticket.categorise().
  • Add an "account" branch for passwords, and a third ticket that uses it.
PreviousClasses

Little by little, you're building something great.