Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.
Default Arguments:
Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the formkeywordname=value.
Syntax:
def function_name(param1, param2=default_value2, param3=default_value3)
Let's understand this through a functionstudent. The functionstudentcontains 3-arguments out of which 2 arguments are assigned with default values. So, the functionstudentaccepts one required argument (firstname), and rest two arguments are optional.
Pythondefstudent(firstname,lastname='Mark',standard='Fifth'):print(firstname,lastname,'studies in',standard,'Standard')
We need to keep the following points in mind while calling functions:
- In the case of passing the keyword arguments, the order of arguments is not important.
- There should be only one value for one parameter.
- The passed keyword name should match with the actual keyword name.
- In the case of calling a function containing non-keyword arguments, the order is important.
Example #1:Calling functions without keyword arguments
Pythondefstudent(firstname,lastname='Mark',standard='Fifth'):print(firstname,lastname,'studies in',standard,'Standard')# 1 positional argumentstudent('John')# 3 positional argumentsstudent('John','Gates','Seventh')# 2 positional argumentsstudent('John','Gates')student('John','Seventh')
Output:
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
In the first call, there is only one required argument and the rest arguments use the default values. In the second call,lastnameand standard arguments value is replaced from default value to new passing value. We can see the order of arguments is important from the 2nd, 3rd, and 4th calls of the function.
Example #2: Calling functions with keyword arguments
Pythondefstudent(firstname,lastname='Mark',standard='Fifth'):print(firstname,lastname,'studies in',standard,'Standard')# 1 keyword argumentstudent(firstname='John')# 2 keyword argumentsstudent(firstname='John',standard='Seventh')# 2 keyword argumentsstudent(lastname='Gates',firstname='John')
Output:
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
In the first call, there is only one required keyword argument. In the second call, one is a required argument and one is optional(standard), whose value gets replaced from default to a new passing value. In the third call, we can see that order in keyword argument is not important.
Example #3: Some Invalid function calls
Pythondefstudent(firstname,lastname='Mark',standard='Fifth'):print(firstname,lastname,'studies in',standard,'Standard')# required argument missingstudent()# non keyword argument after a keyword argumentstudent(firstname='John','Seventh')# unknown keyword argumentstudent(subject='Maths')
The above code will throw an error because:
- In the first call, value is not passed for parameterfirstnamewhich is the required parameter.
- In the second call, there is a non-keyword argument after a keyword argument.
- In the third call, the passing keyword argument is not matched with the actual keyword name arguments.
Using mutable objects as default argument values in python
This must be done very carefully. The reason is the default values of the arguments are evaluated only once when the control reaches the function
Definition for the first time. After that, the same values(or mutable objects) are referenced in the subsequent function calls.
Things will be much more clear with the example
Python# mutable default argument values example using python list# itemName is the name of the item that we want to add to list# that is being passed, or if it is not passed then appending in# the default listdefappendItem(itemName,itemList=[]):itemList.append(itemName)returnitemListprint(appendItem('notebook'))print(appendItem('pencil'))print(appendItem('eraser'))
Output['notebook']['notebook', 'pencil']['notebook', 'pencil', 'eraser']
What you have expected if you assume that a new list is created in each function call when we don't pass a list to it
['notebook']
['pencil']
['eraser']
But as you can see in the actual output of the program every time the function is called, the same list is used, no new list is made on a new call.
Example using dictionary
Python# mutable default argument values example using python dictionary# itemName is the name of item and quantity is the number of such# items are theredefaddItemToDictionary(itemName,quantity,itemList={}):itemList[itemName]=quantityreturnitemListprint(addItemToDictionary('notebook',4))print(addItemToDictionary('pencil',1))print(addItemToDictionary('eraser',1))
Output{'notebook': 4}{'notebook': 4, 'pencil': 1}{'notebook': 4, 'pencil': 1, 'eraser': 1}
What you have expected if you assume that a new dictionary is created in each function call
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
But you can clearly see the actual output of the program is different and it indicates the use of the same dictionary in each subsequent call.
The key takeaway here is we should avoid such scenarios.
Best Practices
Assign the default value as none and then check in the function if the expected list or dictionary argument is none or not.
If it is none then assign it with a list or dictionary depending on your requirement.
Python# using None as values of the default argumentsprint('#list')defappendItem(itemName,itemList=None):ifitemList==None:itemList=[]itemList.append(itemName)returnitemListprint(appendItem('notebook'))print(appendItem('pencil'))print(appendItem('eraser'))# using None as value of default parameterprint('\n\n#dictionary')defaddItemToDictionary(itemName,quantity,itemList=None):ifitemList==None:itemList={}itemList[itemName]=quantityreturnitemListprint(addItemToDictionary('notebook',4))print(addItemToDictionary('pencil',1))print(addItemToDictionary('eraser',1))
Output#list['notebook']['pencil']['eraser']#dictionary{'notebook': 4}{'pencil': 1}{'eraser': 1}
Here you can clearly see that every time a function is called and a list or dictionary is not passed as an argument to the function then it creates a new list or dictionary.