Open In App

Define and Call Methods in a Python Class

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, provides a straightforward syntax for defining and calling methods within a class. In this article, we will explore the concepts of methods in a class in Python and will see how to define and call methods in a class with examples and explanations.

What are Methods in a Class in Python?

A method in a class is a function that is associated with an object. It defines the behavior and actions that objects of the class can perform. Methods are essential for encapsulating functionality and promoting code reusability. They are defined inside a class and can access the attributes (variables) of the class.

Syntax of Method

class ClassName:

def method_name(self, parameter1, parameter2, …):

# Method body – code goes here

# Creating an object of the class

object_name = ClassName()

# Calling a method on the object

object_name.method_name(argument1, argument2, …)

Define And Call Methods In A Class In Python

Let’s look at how to define and call methods in a class with the help of examples.

Example 1: Simple Class with a Method

In this example, we define a class GeeksClass with a method say_hello. The say_hello method simply prints the message “Hello, Geeks!” when called. We then create an instance of the class (geeks_object) and call the say_hello method on that instance.

Python3




class GeeksClass:
    def say_hello(self):
        print("Hello, Geeks!")
 
# Creating an object of GeeksClass
geeks_object = GeeksClass()
 
# Calling the say_hello method
geeks_object.say_hello()


Output

Hello, Geeks!


Example 2: Class with Parameterized Method

In this example, we define a Calculator class with an add method that takes two parameters (num1 and num2) and returns their sum. We create an instance of the Calculator class (calculator_object) and call the add method with arguments 5 and 7.

Python3




class Calculator:
    def add(self, num1, num2):
        return num1 + num2
 
# Creating an object of Calculator
calculator_object = Calculator()
 
# Calling the add method with parameters
result = calculator_object.add(5, 7)
print("Result of addition:", result)


Output

Result of addition: 12


Conclusion

Defining and calling methods in a class in Python is fundamental to object-oriented programming. Methods enable us to encapsulate functionality within objects, promoting code organization and reusability. By understanding how to define and call methods, you can build powerful and modular code structures in Python. As demonstrated with GeeksforGeeks examples, these concepts are not only fundamental but also applicable in real-world coding scenarios.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads