Python Methods vs Functions – What really differentiates them?
So far we have gained an understanding of both methods and functions. And usually, every beginner tends to get a little confused over how a function is different from a method. Well, both of them have one sole purpose in life, to perform some operation. And both of them can return some value. So if they sound so similar, in what ways are they actually different? Today this article will guide you towards Python Methods vs Functions.
But before we do, we quickly need to recall the two of them that is, Python Functions and Python Methods separately.
Simply put, a function is a series of steps executed as a single unit and “encapsulated” under a single name. It is a group of statements capable of performing some tasks.
A function in a program can take arguments (that is, the data to operate on) and can optionally return some data after performing the intended task.
Dive a little deep into the concept of Python Functions in detail with techvidvan.
Creating a function is basically giving your computer well-defined instructions to perform a task. So we can create a simple function in 3 simple steps:
1. def add()
says, hey computer, give me a function called add.
2. def add(a, b)
is saying, hey computer, the add function should take two arguments. This is where the concept of code-reusability comes in. By giving the arguments generalized names a and b, we’re saying that in some instances a can be 2 and b can be 3, while in some other instance a can be 10 and b can be 30.
3. def add(a, b)
This says, hey computer, let my function return some stuff, let it return the sum of a and b.
Finally, you call the function as:
This returns the sum of 10 and 30, that is, 40 to us.
Types of Python Functions
Functions in Python, like many other programming languages, are of two types:
1. Built-in functions
Built-in functions come included with the rich standard library of Python. These are the functions that some programmers somewhere in the world defined a long time ago and now you can use these functions in your program without having to write them all over again.
Explore a little more about Python Built-in Functions with Techvidvan.
Like the print() function which prints some output on the screen.
>>> print ("Techvidvan")
2. User-defined functions
Python lets us define and use our own functions which we can use anywhere in our program. One such example is the add() function we created above.
>>> def add(a, b): Return a + b >>> print(add(10, 30))
Objects in object-oriented programming have attributes, i.e data values within them. But how do we access these attribute values? We access them through methods. Methods represent the behaviour of an Object.
A method is a piece of code that is associated with an object and operates upon the data of that object.
In most respects, a method is identical to a function. Except for two major differences:
- It is associated with an object and we call it ‘on’ that object.
- It operates on the data which is contained within the class.
(Recall that class is a template and object is an instance of a class)
Since a method is associated with an object, defining and creating a method is little different from defining functions:
- We always define a method inside a class.
- A method definition always includes ‘self’ as its first parameter.
- And lastly, we call a method on an object using the dot operator.
Its the right time to get familiar with the concept of Python Methods.
Let’s look at an example to gain a better understanding of methods.
class Car: def sound(self): print "Vroooom!" ferrari = Car() ferrari.sound() #calling method sound on object ferrari
In this example, we define a method sound inside a class Car.
We have then created an object ferrari of the class Car. And we finally call the method sound() on the object ferrari.
Let’s now highlight the differences we have learned.
Difference between Python Methods vs Functions
|Methods definitions are always present inside a class.||We don’t need a class to define a function.|
|Methods are associated with the objects of the class they belong to.||Functions are not associated with any object.|
|A method is called ‘on’ an object. We cannot invoke it just by its name||We can invoke a function just by its name.|
|Methods can operate on the data of the object they associate with||Functions operate on the data you pass to them as arguments.|
|Methods are dependent on the class they belong to.||Functions are independent entities in a program.|
|A method requires to have ‘self’ as its first argument.||Functions do not require any ‘self’ argument. They can have zero or more arguments.|
We can conclude that Methods and Functions are like those two very similar options that appear in your Multiple Choice Questions. They both appear to be similar when you see them but have key differences in what they actually are. In a nutshell, both methods and functions perform tasks and may return some value. But the difference lies in the fact that methods are ‘associated’ with objects, while functions are not.
That was all about Python Methods vs Functions. Thank you for reading our article. Do share it on social media as well.
Keep Learning 🙂