Site icon TechVidvan

Python Methods – Learn to Create & Add a Method to Class

As we all have learned by now, Python supports object-oriented programming. That basically means Python programs can have classes and objects.

Moreover, classes let you bundle behavior and state together. This is where methods come into the picture.

Methods are a way to implement and define a class’s behavior. So methods can be thought of as functions defined within a class.

Python Methods

A method in object-oriented programming is a procedure that represents the behavior of a class and is associated with the objects of that class.

Let’s understand the working of methods at a greater depth.

But before that, we need to create a class.

class Display:
    pass

Let’s break this code:

class Display:
pass

The class looks empty right? Well, let’s add some behavior to this class.

Adding Python Methods to a class

Let’s remove the pass keyword and add a method to our Display class.

class Display:
    def show(self):
        print("Hi from Techvidvan!")

This entire thing which is written inside the class suite is a method.

def show(self):
print("Hi from Techvidvan!")

That was about adding a python method.

Now let’s see how to create objects and use this method.

Invoking Python Method

To invoke a method, we first need to create an object of the class. We can then invoke the method on the newly created object.

Go to the Python shell and type the following:

>>> d = Display()

This creates an object d of class Display.

Now, invoke the method show() on the object d.

>>> d.show()

We call a method on an object using the dot notation.

The output is as follows:

>>> d = Display()
>>> d.show()
Hi from Techvidvan!
>>>

Notice that while defining the method we passed an argument self, but while calling the method we didn’t pass any value to that argument. This brings us to our next section, that is, the significance of self.

The significance of “self”

When we invoke the method as :

d.show()

the interpreter turns this line of code into something like this :

Display.show(d)

where,

Display is the class name, show is the method name, and d is the object name, passed as an argument to show().

Clearly, the interpreter assigns the value of “d” to “self”. This is why every method needs its first parameter to be “self”.

Now that you’ve understood the importance of self, let’s create a method that does something quite more than just printing some string.

class Display:
    def __init__(self, val):
        self.val = val
    def increase(self):
        self.val += 1
    def show(self):
        print(self.val)

Inside the class, there are three methods namely __init__(), increase(), show().

The class also has an instance variable val.

This is what the method call outputs:

>>> c = Display(100)
>>> c.inscrease()
>>> c.show()
101
>>>

Notice that the value of the instance variable val is passed while creating the object c.

Summary

So that is how methods work in python.

We learnt that methods are a past of class and are associated with objects of that class. The data of an object can be accessed only by the methods of that object. We learnt how to create and add a method to a class, how to call that method by creating an object.

We also learnt what the interpreter reads when you call a method.

Exit mobile version