In this tutorial, we will learn how to call a method in Python. Functions and methods are essential in any programming language, as they allow us to break down complex tasks into smaller, more manageable parts.
We will discuss the difference between functions and methods, how to define a method inside a Python class, and how to call a method inside the same or different Python classes.
Step 1: Understand the difference between functions and methods
First, let’s clarify the difference between functions and methods in Python.
A function is a group of related statements that perform a specific task. Functions help break a program into smaller and modular chunks, making it more organized and manageable. They are reusable and make the code more efficient.
A method is similar to a function but is associated with an object or a class. It is called on an object and can access and modify the object’s data. Methods are an essential part of object-oriented programming (OOP).
Step 2: Define a method inside a Python class
To define a method, we first need to create a Python class. A class is a code template for creating objects, and it defines properties and behaviors that the objects created from that class will have.
Here is an example of a simple class Person
with a method greet
:
1 2 3 4 5 6 7 |
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.") |
In this example, we have defined a Person
class with an __init__
method and a greet
method. The __init__
method is a special method called a constructor which is called when an object is created. It initializes the object’s attributes.
The greet
method is a user-defined method that prints out a message introducing the person.
Step 3: Create an object of the class
To call a method, we first need an object of the class. An object is an instance of a class. We create an object like this:
1 |
person1 = Person("John", 30) |
Here, we have created an object person1
of the Person
class and provided the required arguments for the __init__
method.
Step 4: Call the method on the object
Now that we have an object, we can call the greet
method on it. Here’s how to do it:
1 |
person1.greet() |
When we run this code, it will call the greet
method on the person1
object, and we will see the following output:
Hello, my name is John and I am 30 years old.
Step 5: Calling a method from another class
To call a method from another class, you need to create an object of that class and use the object to call the method.
For example, let’s create a new class Dog
with a method bark
, and then call the greet
method of the Person
class from the bark
method.
1 2 3 4 5 6 7 8 |
class Dog: def bark(self): print("Woof! Woof!") person1 = Person("Alice", 25) person1.greet() dog1 = Dog() dog1.bark() |
When we run this code, we will see the following output:
Woof! Woof! Hello, my name is Alice and I am 25 years old.
Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.") class Dog: def bark(self): print("Woof! Woof!") person1 = Person("Alice", 25) person1.greet() person1 = Person("John", 30) person1.greet() dog1 = Dog() dog1.bark() |
Conclusion
In this tutorial, we learned how to call a method in Python. We discussed the difference between functions and methods, and we went through the steps to define a method inside a Python class, create an object of the class, and call the method on the object. Additionally, we explored how to call a method from another class. Understanding how to call methods is crucial in Python, as it enables us to organize code effectively and make use of object-oriented programming.