Python Function Arguments
Function add is defined with parameters a and b:
def add(a, b): return a + b
Arguments are the values passed to a function.
5 and 7 are arguments:
result = add(5, 7) print(result)
Output:
12
Function Argument Types
In Python there are four function argument types:
- Positional arguments: arguments that follow specific order.
- Default arguments: fall back arguments if none provided.
- Keyword arguments: can rewriting order of positional arguments.
- Arbitrary arguments: function is defined to take an undetermined number of parameters. Arbitrary parameter name is preceded by star (*) character.
Tags above will be converted to Title and Author info. It's recommended to keep author tag so that readers who come from search engine traffic can also discover your bio.
Note: You can use regular HTML in this article but some things will be removed like JavaScript and script execution.
Positional Arguments
Positional arguments are just the regular arguments.
The function's positional arguments are passed to its parameter names in the same order they appear in function's definition:
def test(number, list, string): print(a, b, c) test(1, ['a', 'x'], 'hello')
Output:
1 ['a', 'x'] hello
Here number, list and string values are passed to the positional argument list in the same order in which they appear in the function's definition.
Default Arguments
Python's default arguments are provided as fall back values in case none were supplied by the function call.
def say(name = "John", message = "Hello"): print(f"{name} : {message}")
Running the function with empty argument list:
say()
Output:
John : Hello
The function gracefully falls back on default values. Let's see what happens when you pass partial arguments to the same function. Here we will pass name argument and leave message blank:
say("Steve")
Output:
Steve: Hello
Or you can overwrite both arguments:
say("Steve", "Goodbye")
Output:
Steve : Goodbye
Keyword Arguments
Keyword arguments are almost like default arguments in reverse. You provide them when arguments are being passed to the function and not in its definition:
def say(name, message): print(f"{name} : {message}")
To call this function with keyword arguments:
say(name = "John", message = "Hello")
Output is as follows:
John : Hello
Mixing keywords and default arguments
It's possible to mix default and keyword arguments together. But the only proper way of doing that is to prioritize non-keyword parameters in the function definition:
def say(name, message = "Greetings"):
Here positional non-keyword parameter name is listed first without a default value.
This is acceptable and it works as expected. After setting name to "Steve" parameter message is automatically assigned to "Greetings" by default:
Steve : Greetings
Arbitrary Arguments
Arbitrary arguments are used in case when the number of arguments that will be supplied to the function is not known. They are not explicitly defined in function's definition. In some way they can be thought of as optional arguments.
Arbitrary parameters are defined by using the star * character. Not to be mistaken for memory pointer from the C language:
def test(*arb): print(arb)
If none of the arguments are passed:
test()
Output will be an empty tuple:
()
The result is an empty tuple which is one of the many data types in Python.
You can iterate over all arguments with a for in loop;
def list(*arguments): for value in arguments: print(value)
Now let's pass some common animal names:
list("Fox", "Wolf", "Coyote", "Bear", "Rabbit")
Output:
FoxWolf
CoyoteBear
Rabbit
Since arbitrary arguments are iterable you can use for loops on them.