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.
.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.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())<bound method Car.self_driving of <__main__.Car object at 0x106588ec0>> This is a petrol car
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:
class Name:
def action(self):
... # self is the object the method was called on
obj.action()Adding a categorise method
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)None billing
__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
def summary(self):
return f"#{self.id} {self.customer}: {self.category}"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())#1 Asha: billing #2 Ben: shipping
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
class Ticket:
def __init__(self, text):
self.text = text
def shout():
return "TICKET"
Ticket("refund").shout()Traceback (most recent call last):
File "main.py", line 8, in <module>
Ticket("refund").shout()
TypeError: Ticket.shout() takes 0 positional arguments but 1 was givenPython 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
| Function | Method | |
|---|---|---|
| Defined | At the top level of a file | Inside a class |
| Called as | categorise(text) | ticket.categorise() |
| Gets its data from | Its arguments | self, 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.
ticket.summary without brackets does not run the method. It prints something like <bound method Ticket.summary ...>; add () to call it.Related
- Previous: Classes
- Next: dataclasses
- Reference: Method Objects in the Python tutorial
- Add a method
is_urgent(self)that returnsTruefor billing tickets. - Call
ticket.summary()beforeticket.categorise(). - Add an
"account"branch for passwords, and a third ticket that uses it.
Little by little, you're building something great.