Up to this point, we have been calling functions mainly with positional arguments.
But in Python, functions can also have named or keyword arguments. What does that mean? Let us find out by looking at the following example:
def bar(length, char1, char2): return (char1 + char2) * length + char1 print(bar(5, '-', '*')) # => -*-*-*-*-*-
bar function has three arguments. The function call also looks easy. Let us assume that
char2 often get the same values. In such a situation, it is convenient to specify default values:
def bar(length, char1='-', char2='*'): return (char1 + char2) * length + char1 print(bar(5)) # => -*-*-*-*-*- print(bar(3, '.')) # => .*.*.*. print(bar(2, ':', '|')) # => :|:|:
It looks convenient. But what if we are satisfied with the default value for
char1 and set our value for
char2? Will we have to specify both? You do not have to do it unless there is a rare exception.
We can specify any positional argument by name: