4
Lesson 4
Positional and keyword arguments
In Python, when calling a function, you can pass arguments in two main ways: positional arguments and keyword arguments. Understanding the difference between these two types of arguments helps you write more flexible and readable code.
1. Positional arguments:
Positional arguments are the most common way to pass arguments to a function. When you use positional arguments, the order of the arguments matters because they are assigned to parameters based on their position in the function call.
1. Positional arguments:
Positional arguments are the most common way to pass arguments to a function. When you use positional arguments, the order of the arguments matters because they are assigned to parameters based on their position in the function call.
def describe_pet(name, species): print(f"{name} is a {species}.") describe_pet("Buddy", "dog") # Output: Buddy is a dog. describe_pet("Whiskers", "cat") # Output: Whiskers is a cat.
In this example:
- describe_pet("Buddy", "dog") assigns "Buddy" to name and "dog" to species based on their positions.
- Changing the order of arguments would change the output, which might not make sense if reversed: describe_pet("dog", "Buddy") would print "dog is a Buddy."
Positional arguments are useful for short, simple function calls, but if there are many parameters, they can become confusing and hard to read.
2. Keyword arguments:
With keyword arguments, you specify the parameter name along with the value when calling the function. This way, the order of arguments doesn’t matter, and each argument is explicitly matched to its parameter by name.
def describe_pet(name, species): print(f"{name} is a {species}.") describe_pet(name="Buddy", species="dog") # Output: Buddy is a dog. describe_pet(species="cat", name="Whiskers") # Output: Whiskers is a cat.
In this example:
- describe_pet(name="Buddy", species="dog") assigns "Buddy" to name and "dog" to species.
- The order doesn’t matter, so describe_pet(species="cat", name="Whiskers") produces the same result as if the arguments were in the original order.
Keyword arguments improve readability and make it clear what each argument represents, especially when there are many parameters. They also prevent mistakes when accidentally passing arguments in the wrong order.
3. Using both positional and keyword arguments:
You can use a mix of positional and keyword arguments when calling a function. However, positional arguments must come before any keyword arguments.
def describe_pet(name, species, age): print(f"{name} is a {age}-year-old {species}.") describe_pet("Buddy", "dog", age=5) # Output: Buddy is a 5-year-old dog.
In this case:
- "Buddy" and "dog" are positional arguments for name and species.
- age=5 is a keyword argument, explicitly assigning 5 to age.
Trying to place a keyword argument before a positional argument would result in a syntax error:
describe_pet(name="Buddy", "dog", 5) # SyntaxError
4. Default parameter values with keyword arguments:
Keyword arguments are often used with default parameter values in function definitions. This allows you to call the function without providing values for every parameter, as some parameters have predefined default values.
def describe_pet(name, species="dog"): print(f"{name} is a {species}.") describe_pet("Buddy") # Output: Buddy is a dog. describe_pet("Whiskers", species="cat") # Output: Whiskers is a cat.
In this example:
- The species parameter has a default value of "dog", so if it’s not specified, it defaults to "dog".
- You can still use a keyword argument to override the default, as with species="cat".
Summary
- Positional arguments: Assigned to parameters based on order, so the order matters.
- Keyword arguments: Assigned to parameters by explicitly naming them, so the order doesn’t matter.
- Mixing: Positional arguments must come before keyword arguments in a function call.
- Default values: Can be specified in function definitions and are often used with keyword arguments for flexibility.
Using keyword arguments can make function calls more readable and allow you to control arguments better, especially in complex functions.