Define and Call Methods in a Python Class
Last Updated :
14 Feb, 2024
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()
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)
OutputResult 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.
Similar Reads
Call a Class Method From another Class in Python In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method f
3 min read
Extend Class Method in Python In Python, class methods are functions that are bound to the class rather than the instance of the class. This means they receive the class (cls) as the first argument instead of the instance (self). Extending a class method refers to enhancing or customizing the behavior of an inherited class metho
3 min read
How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde
3 min read
Call Parent class method - Python In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effe
5 min read
Accessing Attributes and Methods in Python In Python, attributes and methods define an object's behavior and encapsulate data within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes
3 min read
Python Classes and Objects A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.Classes are created using class ke
6 min read