Related article:
Transcript:
Let's talk about the two types of arguments you can use when calling a function: positional arguments and named arguments (a.k.a. keyword arguments).
Positional arguments
When you call the built-in print
function, you can pass in any number of arguments positionally. We're passing in four positional arguments here:
>>> print(2, 1, 3, 4)
2 1 3 4
We call these positional arguments because their position matters. The order of these arguments is significant: the first argument is printed out first; the last one is printed out last.
Keyword arguments (a.k.a. named arguments)
The print
function also accepts some arguments as keyword arguments.
The print
function accepts an optional sep
argument (which defaults to a space character).
>>> print(2, 1, 3, 4, sep=' ')
2 1 3 4
>>> print(2, 1, 3, 4)
2 1 3 4
>>> print(2, 1, 3, 4, sep='-')
2-1-3-4
>>> print(2, 1, 3, 4, sep=', ')
2, 1, 3, 4
That sep
argument defines the separator that should be printed-out between each of the positional arguments given to print
.
There's also an optional end
keyword argument. The end
argument defaults to a newline character:
>>> print(2, 1, 3, 4, sep=', ', end='\n')
2, 1, 3, 4
But we can put some exclamation marks in the end
argument (before a newline) to print out exclamation marks at the end:
>>> print(2, 1, 3, 4, sep=', ', end='!!\n')
2, 1, 3, 4!!
The order of keyword arguments doesn't matter
The order of the print
function's sep
and end
arguments doesn't actually matter.
>>> print(2, 1, 3, 4, end='!!\n', sep=', ')
2, 1, 3, 4!!
The order doesn't matter with these because they're not positional arguments: they're named arguments.
Positional arguments have commas between their values.
>>> print(2, 1, 3, 4)
2 1 3 4
Keyword arguments (a.k.a. named arguments) have a name and an equals sign in addition to those values and commas.
>>> print(2, 1, 3, 4, sep=',', end='!\n')
2,1,3,4!
Keyword arguments must come after any positional arguments. Beyond that, the position of keyword arguments doesn't matter at all: it's the name that matters not the position.
Using keyword arguments instead of positional arguments
Keyword arguments aren't just useful for functions that accept any number of positional arguments (like print
). You can pass keyword arguments to just about any function in Python.
For example, the built-in sum
function accepts a first argument:
>>> sum([2, 1, 3 ,4])
10
But it also accepts a second argument, which defaults to zero:
>>> sum([2, 1, 3 ,4], 0)
10
If we change that second argument to 1
, we'll see that this is the start value for the returned summation:
>>> sum([2, 1, 3 ,4], 1)
11
I would much rather see this function called like this:
>>> sum([2, 1, 3 ,4], start=1)
11
We're passing in one positional argument and one keyword argument.
That start=1
works with sum
because start
is the name of that argument. In the documentation for the sum
function it says the second argument that is called start
:
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
So when you're working with named arguments (a.k.a. keyword arguments) the name of the argument actually matters! Whereas when you're working with positional arguments, it's the position that's significant.
Summary
When we call a function in Python, we can pass in two different types of arguments:
- positional arguments
- named arguments (a.k.a. keyword arguments)
Named arguments can sometimes make your code a bit more descriptive because you've given a name to an object whose use might not otherwise be clear simply by its position in a function call.
from Planet Python
via read more
No comments:
Post a Comment