Introduction to Python – Function Arguments
- An argument (also known as an arg) is data that can be push into a function.
- The function must be called with the exact number of arguments that it is expecting. Notice the error received.
- The code also uses the ‘in’ keyword. This keyword tests whether or not a sequence contains a specific value. The values are evaluated at the function definition within the defining scope.
- Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed. One can think of the keyword arguments (kwargs) as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.
- The form is kwarg=value
- Now notice when we call the function with the proper number of arguments we no longer receive the error.
- ***The reason we are not having to pass the ‘reminder’ argument is explained next.***
- Default arguments are used so that a function can be called with fewer arguments than it is defined to allow.
- In the past example we needed to pass the argument ‘retries’ or else we would receive an error.
- Now if we change our default value the number of times the function iterates is different. Why?
- Notice that the default value is evaluated only once.
- when the default is a mutable object such as a list, dictionary, or instances of most classes the function accumulates the arguments passed to it on subsequent calls.