Function Overloading
Function overloading means using the same function name with different numbers or types of parameters.
For example, imagine ordering food:
Situation 1: Cooking at home
- Go to the kitchen.
- Take the ingredients.
- Prepare the food.
Situation 2: Ordering food from a hotel
- Go to the hotel or restaurant.
- Order the food.
- Get the prepared food.
The task is the same (getting food), but the process is different.
Similarly, in programming, function overloading allows us to use the same function name for different operations.
Example:
def add(no1, no2):
print(no1 + no2)
def add(no1, no2, no3):
print(no1 + no2 + no3)
add(10, 20, 30) # 60
In Python, the second add() definition replaces the first one. So calling add(10, 20) will result in an error.
Does Python support function overloading?
Python does not support traditional function overloading based on different parameter lists like Java.
However, we can achieve similar behavior using:
- Default arguments
- Variable-length arguments (*args)
- Decorators or libraries such as multipledispatch
Dynamically Typed Language
Python is a dynamically typed language.
It means we do not need to declare the data type of a variable explicitly. Python determines the type based on the value assigned to it.
Example in Java (Statically Typed):
int no1 = 10;
float no2 = 2.3f;
boolean raining = false;
In Java, we need to specify the data type when declaring variables.
Example in Python (Dynamically Typed):
no1 = 10
no2 = 20
print(no1)
print(no2)
We can also assign a different type of value to the same variable.
no1 = 10
print(type(no1)) # <class 'int'>
no1 = "Python"
print(type(no1)) # <class 'str'>
Python automatically determines the type based on the assigned value.
Duck Typing
Python is also known for supporting Duck Typing.
There is a popular saying:
"If it looks like a duck and quacks like a duck, it is probably a duck."
In programming, duck typing means that an object's behavior is more important than its specific type.
For example, if an object has a method called quack(), we can use that method without checking whether the object belongs to a particular class.
Example:
class Duck:
def sound(self):
print("Quack Quack")
class Person:
def sound(self):
print("I can imitate a duck")
def make_sound(obj):
obj.sound()
duck = Duck()
person = Person()
make_sound(duck)
make_sound(person)
Output:
Quack Quack
I can imitate a duck
Here, both objects have a sound() method. The function works with both objects because they support the required behavior.
This is the basic idea of Duck Typing in Python.
Arguments in Python
Arguments are the values passed to a function when calling it.
For example:
def add(no1, no2):
print(no1 + no2)
add(100, 200)
Here:
- no1 and no2 are parameters.
- 100 and 200 are arguments.
Types of Arguments
1.Default Argument
2.Keyword Argument
3.Positional Argument
4.Arbitary Argument(variable-length arguments *args and **kwargs)
5.lambda function argument
1.Default Arguments
Default arguments are parameters that have default values.
If we do not pass a value for a parameter, Python uses its default value.
Example 1:
def login(username='admin', password='admin'):
print(username, password)
login()
login('abcd', 'abcd134')
Output:
admin admin
abcd abcd134
Here, both parameters have default values. If we don't pass arguments, Python uses those default values.
Example 2:
def login(username, password='admin'):
print(username, password)
login('abcd', 'abcd134')
login('abcde')
Output:
abcd abcd134
abcde admin
Here:
- username is a required parameter.
- password has a default value of 'admin'.
When we pass only one argument, Python uses the default password.
Important Rule:
Parameters with default values should come after parameters without default values.
Correct:
def wish(name, message='Happy Birthday'):
print(name, message)
wish('Subash')
Output:
Subash Happy Birthday
Incorrect:
def wish(name='Guest', message):
print(name, message)
This produces:
SyntaxError: non-default argument follows default argument
In simple words, a required parameter should not come after a parameter with a default value.
Positional Arguments
Positional arguments are passed to a function based on their position or order.
Example:
def add(no1, no2):
print(no1 + no2)
add(10, 20)
Here:
- 10 is assigned to no1.
- 20 is assigned to no2.
Output:
30
Keyword Arguments
Keyword arguments are passed using the parameter names. We explicitly mention which value belongs to which parameter.
Example:
def greet(name, message):
print(name, message)
greet(name='Narmatha', message='Welcome home')
Output:
Narmatha Welcome home
We can also change the order of keyword arguments.
greet(message='Welcome home', name='Narmatha')
Output:
Narmatha Welcome home
Mixing Positional and Keyword Arguments:
We can also combine positional and keyword arguments.
greet('Narmatha', message='Welcome home')
Output:
Narmatha Welcome home
Here:
'Narmatha' is passed as a positional argument.
message='Welcome home' is passed as a keyword argument.
Incorrect Example:
greet(message='Welcome home', 'Narmatha')
This produces a SyntaxError because a positional argument cannot follow a keyword argument.
Remember:
- Positional arguments depend on their position.
- Default arguments use a predefined value when no value is supplied.
- Keyword arguments use parameter names to assign values.
Variable-Length Arguments
Sometimes we do not know how many arguments will be passed to a function.
Python provides *args to handle a variable number of positional arguments.
Example 1:
def calculate_total(sub,*subjects):
print(subjects)
calculate_total(100, 98, 95, 97, 45)
calculate_total(100, 200)
calculate_total(100)
Output:
(98, 95, 97, 45)
(200)
()
Example 2:
def calculate_total(*subjects,sub):
print(subjects)
calculate_total(100, 98, 95, 97, 45)
calculate_total(100, 200)
calculate_total(100)
Output:
Error
Here:
- *subjects collects all the positional arguments into a tuple.
- We can pass any number of arguments.
- Even if we do not pass any arguments, the function works.
Top comments (0)