This content originally appeared on DEV Community and was authored by Juwon?🍀
In this article we aim to discuss the difference between FUNCTIONS AND METHODS IN PYTHON PROGAMMING, to have a clear understanding of both.
To begin with;
WHAT ARE FUNCTIONS IN PYTHON
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 :
- Name
- Argument
- 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.
Syntax
#python
li = [1,2,3]
ans = sum(li)
print(ans)
Output:
6
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 own function to fulfill their specific needs.
Creating a User-Defined Function In Python
We can create a function using the keyword def.
Syntax
#python
def function_name(argument):
“ “ “
functions logics
” ” ”
return values
Example:
#python
def join(str1, str2):
joined_str = str1 + str2
return joined_str
print(join)
Output:
<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.
Syntax
#python
function_name(arguments)
Example:
#python
print(join("Hello", "World"))
Output:
HelloWorld
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.
Example:
#python
li = [1,2,3]
new = list(map(lambda x:x+1, li))
print(new)
Output:
[2, 3, 4]
WHAT ARE METHODS IN PYTHON
Functions inside a class are called methods. Methods are associated with a class/object. As Python is an Object-Oriented Programming language, so it contains objects, and these objects have different properties and behavior. Methods in Python are used to define the behavior 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 function but this time, it should be inside a class.
Syntax
#python
class ClassName:
def method_name(parameters):
# Statements…
Example:
#python
class Addition:
def add(self, num1, num2):
return num1 + num2
print(Addition.add)
Output:
<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.
Syntax
#python
object_name.method_name(arguments)
Example:
#python
addition1 = Addition() # Object Instantiation
print(addition1.add(2, 3))
Output:
5
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 @classmethod decorator.
- Static Method: A static method doesn’t know it’s a class or an instance, and they do not need to access the class data. It is defined using @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.
CONCLUSION
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.
Keep Learning!!
Keep Sharing!!
This content originally appeared on DEV Community and was authored by Juwon?🍀
Juwon?🍀 | Sciencx (2023-05-10T16:59:56+00:00) DIFFERENCE BETWEEN FUNCTIONS AND METHODS IN PYTHON.. Retrieved from https://www.scien.cx/2023/05/10/difference-between-functions-and-methods-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.