Methods
A method is a function that belongs to a class. Methods define what an object can do.
Defining a Method
Methods are defined inside a class, just like regular functions — but they always take self as the first parameter.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print("Woof!")
def introduce(self):
print("Hi, I'm " + self.name)self gives the method access to the object’s attributes. Without it, introduce wouldn’t know which dog’s name to print.
Calling a Method
Use dot notation to call a method on an object:
dog1 = Dog("Buddy", 3)
dog1.bark() # Woof!
dog1.introduce() # Hi, I'm BuddyYou don’t pass self when calling — Python handles that automatically.
Methods with Parameters
Methods can take extra parameters beyond self:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(self.name + " is eating " + food)dog1 = Dog("Buddy", 3)
dog1.eat("kibble") # Buddy is eating kibbleThis is exactly how Pillow works — draw.circle(...) is calling the circle method on the draw object, passing in the center, radius, and color as arguments.
Methods Can Use Other Methods
A method can call other methods on the same object using self:
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print("Woof!")
def greet(self):
print("Hi, I'm " + self.name)
self.bark()dog1 = Dog("Buddy")
dog1.greet()
# Hi, I'm Buddy
# Woof!Try it out
main.py
Output
Last updated on