12. Methods - An overview
Methods are a set of statements that are called to perform a specific task.
What is the difference between function and method?
The terms function and method are used interchangeably, but if a specific difference is to be noted, a function does not need to be part of a class (this is followed in functional programming) whereas a method (which is a part of Object-oriented Programming) is a function which is always used inside a class to access the variables that hold instance data. So going forward, we shall be using the term method.
How are methods defined inside a class in Python ?
Methods are defined inside a class using the syntax:
class Class_Name: def method1(self, arg1,..., argn): Statement_1 Statement_2 ... .... Statement_n def method2(self): Statement_1 Statement_2 ... .... Statement_n
Methods when called, are generally specific to an instance of the class like the one below:
C1 = Class_Name() C1.method1(1, 2, 3) C1.method2()
Methods cannot be overloaded but can be overridden under inheritance. We shall understand about it in the next section.