In this article, we aim to discuss the difference between FUNCTIONS AND METHODS IN PYTHON PROGRAMMING, to have a clear understanding of both.
To begin with;
A function is a block of code (or statement) that performs a specific task and runs only when called. A function is also a line of code that accomplishes a certain task. Functions have :
- Return Statement Return statement and argument are both optional. A function can either have them or not.
There are mainly three types of FUNCTIONS in Python
- Built-in Function
- User-defined Function
- Anonymous Function
Built-in Function: Built-in functions are the pre-defined functions in Python that can be directly used. We do not need to create them, to use them we just have to call them. Examples: sum(),min(),max(),etc...
An example to show the syntax of built-in functions.
#python li = [1,2,3] ans = sum(li) print(ans)
In the above code example, we used two Built-in functions, sum() and print() to find and output the sum of the list li.
User-Defined Function: They are not pre-defined functions. The user creates their function to fulfil their specific needs.
Creating a User-Defined Function In Python
We can create a function using the keyword def.
#python def function_name(argument): “ “ “ functions logics ” ” ” return values
#python def join(str1, str2): joined_str = str1 + str2 return joined_str print(join)
<function join at 0x000002E42649D750>
In the above code example, we created a function named join which combines its two parameters and returns a concatenated string.
Functions are only executed when they are called.
Calling A Function:
Without calling, a function will never run or be executed. To call a function we use the following syntax.
#python print(join("Hello", "World"))
In the above code example, we executed our function join() by calling it with two arguments “Hello” and “World” and it returned their concatenated string “HelloWorld”.
Anonymous Functions in Python: Functions without a name and are declared without using the keyword def are called Anonymous Functions. To create these functions, we use the keyword lambda and these functions are also called Lambda Functions.
#python li = [1,2,3] new = list(map(lambda x:x+1, li)) print(new)
[2, 3, 4]
Functions inside a class are called methods. Methods are associated with a class/object. As Python is an Object-Oriented Programming language, it contains objects, and these objects have different properties and behaviour. Methods in Python are used to define the behaviour of the Python objects.
- It facilitates code reusability by creating various methods or functions in the program.
- It also improves readability and accessibility to a particular code block.
- Methods creation makes it easy to debug for the programmers.
Creating a Method in Python:
We use the same syntax as the function but this time, it should be inside a class.
#python class ClassName: def method_name(parameters): # Statements…
#python class Addition: def add(self, num1, num2): return num1 + num2 print(Addition.add)
<function Addition.add at 0x1088655e0>
Calling a Method in Python:
To use a method, we need to call it. We call the method just like a function but since methods are associated with class/object, we need to use a class/object name and a dot operator to call it.
#python addition1 = Addition() # Object Instantiation print(addition1.add(2, 3))
In the above code example, we created an object addition1 for our class Addition and used that object to call our method add() with arguments 2 and 3. After calling, our method got executed and returned the value 5.
Being inside a class gives methods a few more abilities. Methods can access the class/object attributes. Since methods can access the class/object attributes, they can also alter them.
Types Of Method In Python:
- Instance Method: It is one of the most common methods in Python, used to set or get details about the instances (objects). Self is the default parameter that points to an instance of the class.
- Class Method: It is used to get the status of the class, but they can’t access or modify the specific instance data. It is defined using the @classmethod decorator.
- Static Method: A static method doesn’t know if it’s a class or an instance, and they do not need to access the class data. It is defined using the @staticmethod decorator.
NOTABLE DIFFERENCES BETWEEN FUNCTION AND METHOD IN PYTHON
- Method definition is always present inside the class, while the class is not required to define the function.
- Functions can have a zero parameter, whereas the method should have a default parameter, either self or cls, to get the object.
- The method operates the data in the class, while a function is used to return or pass the data.
- A function can be directly called by its name, while a method can’t be called by its name.
- The method lies under Object-Oriented Programming, while a function is an independent functionality.
By looking at the above differences, we can simply say that all methods are functions but all functions are not methods.
In this article, we have discussed the functions and methods in Python, the difference between them, and their types.
Hope you will like the article.