Python Optional Parameters

Introduction

Sometimes, you may encounter situations where you want a function to work a certain way most of the time, but also have the option of changing its behavior occasionally. This is where Python optional parameters come in.

Optional parameters allow you to define functions with parameters that have default values. These default values are used when an argument is not provided during the function call.

Benefits of Optional Parameters

Optional parameters provide several advantages:

  • Default Behavior: You can set meaningful default values, ensuring your function can work even if not all arguments are provided.
  • Clearer Code: Optional parameters make your code more concise.
  • Backward Compatibility: You can add new parameters to functions without breaking existing code that doesn’t provide those parameters.
  • Easier Maintenance: Changes to default behavior can be managed centrally within the function definition.

Defining Functions with Optional Parameters

You specify an optional parameter in a function definition. To do so, add an equal sign = followed by the default value after a parameter name.

For example, suppose you are working on software for managing a farm and write a function that creates a description for a farm animal. The description includes the species, animal class and number of legs the animal has.

Most farm animals are mammals (that’s the animal’s class) and have four legs. The function will therefore have those as default values.

def farm_animal_description(species, animal_class="mammal", number_of_legs=4):
    description = f"Farm animal description: This {animal_class} is a {species} and it has {number_of_legs} legs."
    return description

Using Default Arguments In Function Calls

Now you can use the function for a cow, which is a 4-legged mammal, like this:

farm_animal_description("cow")

Output:

'Farm animal description: This mammal is a cow and it has 4 legs.'

To get the cow’s description, you only needed to pass the species argument, and omit the rest.

But if you want to use the function for a chicken, which is a 2-legged bird, you’ll have to pass all the arguments.

Values that are explicitly passed in a function call, override any parameter defaults.

Example:

farm_animal_description("chicken", "bird", 2)

Output:

'Farm animal description: This bird is a chicken and it has 2 legs.'

Passing Some Optional Arguments But Not Others

Suppose we live on a scary farm that raises crocodiles. Crocodiles are not mammals. They are reptiles, but have four legs.

To accommodate crocodiles, pass species, which is a required argument, and animal_type, which is optional, but you have to use it now.

number_of_legs can be omitted since it’s the default of 4:

farm_animal_description("crocodile", "reptile")

Output:

'Farm animal description: This reptile is a crocodile and it has 4 legs.'

But now here is a twist. Suppose the farm is in Australia and includes kangaroos. A kangaroo is a mammal with two legs.

Mammal is the default, but not two legs. In this case, if just the default is omitted, the function will not work properly:

farm_animal_description("kangaroo", 4)

Output:

'This 4 is a kangaroo and it has 4 legs.'

The description includes “4” as the animal class because the function call has a 4 given by the second argument, which is matched with the second parameter, animal_class.

To make this right, you’ll have to use a keyword argument, like this:

farm_animal_description("kangaroo", number_of_legs=4)

Output:

'Farm animal description: This mammal is a kangaroo and it has 4 legs.'

In the function call above, the animal_class parameter was skipped by specifying the next argument using the parameter name, number_of_legs and an equal sign =. An argument like this is called a keyword argument. You’ll find more information about those in the Python Keyword Arguments lesson.

Summary & Reference for Python Optional Parameters

Optional parameters allow you to define functions with default values, ensuring functionality even when not all arguments are provided.


You specify an optional parameter in a function definition. To do so, add an equal sign = followed by the default value after a parameter name.

def farm_animal_description(species, animal_class="mammal", number_of_legs=4):
    description = f"Farm animal description: This {animal_class} is a {species} and it has {number_of_legs} legs."
    return description

Calling a function with optional parameters involves providing only the necessary arguments, as the default values will be used for the rest.

farm_animal_description("cow")  
# "Farm animal description: This mammal is a cow and it has 4 legs."

You can override default values by explicitly passing arguments during the function call.

farm_animal_description("chicken", "bird", 2)  
# "Farm animal description: This bird is a chicken and it has 2 legs."

For partial overrides, you can omit arguments from the right.

farm_animal_description("crocodile", "reptile")  
# "Farm animal description: This reptile is a crocodile and it has 4 legs."

If you want to use the default for an argument in the middle of the list, you can skip it by using keywords for the remaining arguments.

farm_animal_description("kangaroo", number_of_legs=4)  
# "Farm animal description: This mammal is a kangaroo and it has 4 legs."