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 behaviour and state together. This is where methods come into the picture. Methods are a way to implement and define a class’s behaviour. So methods can be thought of as functions defined within a class.
Let’s take a quick revision on Python Classes in detail to clear you basics with Techvidvan.
A method in object-oriented programming is a procedure that represents the behaviour 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:
- The definition of a class starts with the class keyword.
- Display is the name of the class.
- The colon ( : ) signifies the beginning of a suite.
- The pass keyword is used when you want to define a class but you do not yet know what to put inside that class.
The class looks empty right? Well, let’s add some behaviour 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.
- A method, much like a normal function, starts with a def keyword.
- show is the name of our method.
- It takes a single argument self, written inside the parenthesis.
- The colon ( : ) specifies the start of a suite.
print("Hi from Techvidvan!")
- This is the statement written inside the method’s suite.
- It prints the string written inside the parenthesis.
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.
We call a method on an object using the dot notation.
The output is as follows:
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 :
the interpreter turns this line of code into something like this :
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.
The __init__() method is called a constructor method. It initialises the value of all the instance variables of a class.
The increase() method increments the value of the attribute val by 1.
The show() method displays the value of the attribute val.
This is what the method call outputs:
Notice that the value of the instance variable val is passed while creating the object c.
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.
Thank you for reading our article. Do share it on Social Media as well.
Keep Learning 🙂