Published Aug. 3, 2023, 1:21 p.m.
In object-oriented programming, a method is a function that is associated with an object. Methods are used to perform specific actions or operations on an object's data. In Python, methods are defined inside a class and are called on instances of that class to manipulate the object's attributes or perform other actions related to the object.
The syntax to define a method in Python is straightforward. We define a method inside a class, and it takes at least one parameter, which is conventionally named self
. The self
parameter refers to the instance of the class itself, allowing the method to access and modify the object's attributes.
Here's the general syntax to define a method in Python:
class ClassName:
def method_name(self, other_parameters):
# Method body
# Perform actions using self and other_parameters
# Optionally, return a value
class ClassName:
: Defines the class where the method belongs.def method_name(self, other_parameters)
: Defines the method with its name and parameters. The first parameter, self
, is mandatory and represents the instance of the class.# Method body
: Contains the actual implementation of the method.self
: Inside the method, self
is used to access the object's attributes and call other methods of the same class.other_parameters
: Additional parameters can be added to the method as needed, based on the functionality of the method.In this example, we'll create a class called Rectangle
with two methods: calculate_area()
and calculate_perimeter()
. These methods will calculate the area and perimeter of a rectangle, respectively.
class Rectangle:
def __init__(self, length, width):
# Constructor with `self`, `length`, and `width` arguments
self.length = length
self.width = width
def calculate_area(self):
# Method to calculate the area of the rectangle
return self.length * self.width
def calculate_perimeter(self):
# Method to calculate the perimeter of the rectangle
return 2 * (self.length + self.width)
Explanation:
We define a class Rectangle
with a constructor __init__
, which takes three parameters: self
, length
, and width
. The self
parameter is required in all instance methods to refer to the instance itself.
Inside the constructor, we use the self
variable to initialize the instance variables length
and width
.
We define two methods: calculate_area()
and calculate_perimeter()
. Both methods use the self
variable to access the instance attributes length
and width
.
The calculate_area()
method calculates the area of the rectangle by multiplying its length
and width
.
The calculate_perimeter()
method calculates the perimeter of the rectangle using the formula: perimeter = 2 * (length + width).
Now, let's create an instance of the Rectangle
class and call the methods to calculate the area and perimeter.
# Create a rectangle instance using the constructor
rectangle1 = Rectangle(5, 8)
# Call the method to calculate the area
area = rectangle1.calculate_area()
print(f"The area of the rectangle is: {area}")
# Call the method to calculate the perimeter
perimeter = rectangle1.calculate_perimeter()
print(f"The perimeter of the rectangle is: {perimeter}")
Output:
The area of the rectangle is: 40
The perimeter of the rectangle is: 26
In this example, we'll create a class called Employee
with a method calculate_salary()
. This method will take a parameter hours_worked
and calculate the employee's salary based on an hourly rate.
class Employee:
def __init__(self, name, hourly_rate):
# Constructor with `self`, `name`, and `hourly_rate` arguments
self.name = name
self.hourly_rate = hourly_rate
def calculate_salary(self, hours_worked):
# Method to calculate the salary based on hours worked
return self.hourly_rate * hours_worked
Explanation:
We define a class Employee
with a constructor __init__
, which takes three parameters: self
, name
, and hourly_rate
.
Inside the constructor, we use the self
variable to initialize the instance variables name
and hourly_rate
.
We define a method calculate_salary()
that takes the additional parameter hours_worked
. This method calculates the employee's salary by multiplying the hourly_rate
with the hours_worked
.
Now, let's create an instance of the Employee
class and call the method to calculate the salary.
# Create an employee instance using the constructor
employee1 = Employee("John Doe", 20)
# Calculate the salary for 40 hours worked
hours_worked = 40
salary = employee1.calculate_salary(hours_worked)
print(f"{employee1.name}'s salary for {hours_worked} hours worked is: ${salary}")
Output:
John Doe's salary for 40 hours worked is: $800
In this example, we see how the calculate_salary()
method takes a parameter hours_worked
and returns the calculated salary based on the employee's hourly rate. The method allows us to calculate the salary for different hours worked without changing the class's attributes.