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.

def add_employee(employee_id, **degrees):
    print(f'Length of degrees is: {len(degrees)}')
    print(f'Type of degrees is: {type(degrees)}')
    print(degrees)
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'}
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'}
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)
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'}