You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

6.6 Keyword arguments

2 min readjune 24, 2024

Python functions offer versatile ways to pass arguments. rely on order, while use parameter names. This flexibility allows for clearer, more readable code and easier function calls with multiple parameters.

Default parameter values provide convenience and flexibility in function calls. They allow and can be overridden when needed. Keyword arguments enhance readability, flexibility, and maintainability, making complex function calls more manageable and intuitive.

Function Arguments in Python

Positional vs keyword arguments

Top images from around the web for Positional vs keyword arguments
Top images from around the web for Positional vs keyword arguments
  • passed to a function based on their position or order
    • Order of arguments in must match order of parameters in ()
    • def greet(name, age):
      calling
      greet("Alice", 25)
      assigns
      "Alice"
      to
      name
      and
      25
      to
      age
  • Keyword arguments passed to a function using parameter names along with corresponding values
    • Order of arguments doesn't matter as long as parameter names are specified
    • def greet(name, age):
      calling
      greet(age=25, name="Alice")
      achieves same result as
      greet("Alice", 25)
  • Mixing positional and keyword arguments allowed
    • Positional arguments must come before keyword arguments
    • greet("Alice", age=25)
      is valid, but
      greet(name="Alice", 25)
      is not

Default parameter values

  • Specify default value for a parameter in function definition
    • If argument not provided for that parameter when function is called, default value is used
    • def greet(name, age=18):
      if
      age
      not provided when calling
      greet()
      , it defaults to
      18
  • Calling functions with default parameter values
    • Omit arguments for parameters with (
      greet("Alice")
      uses default value of
      18
      for
      age
      )
    • Override default values by providing arguments (
      greet("Alice", 25)
      overrides default
      age
      with
      25
      )
    • Use keyword arguments to specify only parameters you want to override (
      greet("Alice", age=25)
      overrides default
      age
      while keeping positional argument for
      name
      )
  • Default values create optional parameters in the function definition

Benefits of keyword arguments

  • Improved readability
    • Make purpose of each argument clear in function call
    • draw_rectangle(width=10, height=5)
      more readable than
      draw_rectangle(10, 5)
    • Especially useful when function has many parameters or purpose of each argument not immediately clear
  • Flexibility in argument order
    • Allow specifying arguments in any order
    • draw_rectangle(height=5, width=10)
      equivalent to
      draw_rectangle(width=10, height=5)
    • Helpful when function has many optional parameters
  • Selective argument overriding
    • Override only specific default values
    • def create_user(name, age=18, email=None):
      can call
      create_user("Alice", email="alice@example.com")
      to override only
      email
      parameter while keeping default
      age
  • Improved maintainability
    • If function definition changes to add, remove, or reorder parameters, existing function calls with keyword arguments may not need to be updated
    • Makes code more maintainable and less prone to errors when refactoring

Function Definition and Calling

  • Function definition establishes the parameters and their order
  • Function call involves to match the defined parameters
  • The relationship between definition and call determines how arguments are interpreted
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary