Keyword Arguments¶
Let us go through the details related to Keyword Arguments using Python as Programming Language.
- Keyword Argument is same as passing argument by name.
- You can also specify parameter for varying keyword arguments with
**
at the beginning – example:**degrees
- While passing arguments to satisfy the parameter with
**
, you have to pass key as well as value. - Varying Keyword Arguments can be processed as
dict
in the Function body.
In [1]:
def add_employee(employee_id, **degrees):
print(f'Length of degrees is: {len(degrees)}')
print(f'Type of degrees is: {type(degrees)}')
print(degrees)
In [2]:
add_employee(1, bachelors='B. Sc', masters='M. C. A')
Length of degrees is: 2 Type of degrees is: <class 'dict'> {'bachelors': 'B. Sc', 'masters': 'M. C. A'}
In [3]:
degrees = {'bachelors': 'B. Sc', 'masters': 'M. C. A'}
add_employee(1, **degrees)
Length of degrees is: 2 Type of degrees is: <class 'dict'> {'bachelors': 'B. Sc', 'masters': 'M. C. A'}
In [4]:
def add_employee(employee_id, *phone_numbers, **degrees):
print(f'Length of phone_numbers is: {len(phone_numbers)}')
print(f'Type of phone_numbers is: {type(phone_numbers)}')
print(phone_numbers)
print(f'Length of degrees is: {len(degrees)}')
print(f'Type of degrees is: {type(degrees)}')
print(degrees)
In [5]:
add_employee(1, '1234567890', '1234567890', bachelors='B. Sc', masters='M. C. A')
Length of phone_numbers is: 2 Type of phone_numbers is: <class 'tuple'> ('1234567890', '1234567890') Length of degrees is: 2 Type of degrees is: <class 'dict'> {'bachelors': 'B. Sc', 'masters': 'M. C. A'}