5) a. Default Argument Values

Consider the following example:



Here’s something to think about. Pause a moment and look at the last example once again.

Think about what would happen if you called the function but didn’t provide any value to the function, AKA you called the function as just display_greeting(), missing the value. Can you try editing the previous example and find out what happens?

The program terminates with an error! Why? Because you told Python that you will give it one argument while calling the function but didn’t give it anything.



Very often there is a need to set a default value to a function argument. While calling the function if you miss values then the default value will be considered.

Default values for the arguments are provided in the function’s definition itself. Check out the example below and notice how the program executes fine when you call the function without providing any value:



This is the beauty of default argument values. You don’t have to provide all the arguments to a function while calling it.



We’ve been using a lot of print statements so far. This is how the print function is defined in Python. Notice the parameters:

1
 def print(self, *args, sep=' ', end='\n', file=None): ...



Ignore the “self” keyword because that will be covered later under “classes”. For now, focus on sep, end and file . See how they have default values because core Python developers assumed that in most cases you just want to print things with a space in between them (sep= ‘ ‘ ), a newline at the end (end= ‘\n’ ) and to your screen instead of a file (file = None).

If they didn’t set the default values you would have to provide them yourself every single time you wanted to print something. Imagine how annoying that would have been!



Another thing to note while providing default values is that when you define default values in a function, they must be among the last few arguments. They cannot be only the middle few arguments.

For example, these are valid ways of defining:

def calculate(x, y = 0, z = 1)

def display(x, y, z = 1)

These are invalid ways of defining:

def calculate(x = 0, y = 0, z)

def display(x, y = 2, z)

Why is this invalid?

The reason for this is that when you define defaults for the middle arguments but not for ending arguments then it causes ambiguity. Consider the time when we’re calling the last example “display”. We want to provide values for x and z. So we write:

display(3, 5)

But this causes ambiguity. Python will be confused about whether 5 is being assigned to y and you missed z argument or whether you expected 5 as z’s argument and wanted to take the default value of y.

You might think that one of these cases is obvious but each person will have his own opinion. Hence, the syntax doesn’t allow you omit defaults for ending arguments. vr

1
2
Output:
SyntaxError: non-default argument follows default argument on line 1




However not everything can be used as a default value. Consider that you have a function to which want to pass a list. But when you don’t pass the list then you define default to be an empty list. Have a look at this example.

What do you expect the output will be? Turns out it’s this.

1
2
3
4
Output:
['something']
['something', 'something']
['something', 'something', 'something']


Probably not what you expected. But can you guess why did this happen?

You see, in Python every single line of code is executed one by one. Top to bottom and left to right.

When the first line was executed python remembers that now there is a function simple_function that has a parameter with default value of an empty list. But every time you execute this function the same default list will be used. That is why more and more values were added to the list as you ran the code. That principle holds true to any data structure used as a default value.

So what should you do instead? In cases like this it is a Python convention to use None as a default value. You then need to check if items is None and if it is create a new list.


1
2
Output:
['something']