Reputation: 559
The Python Documentation for parameters says the following:
Parameter - A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept... Parameters can specify both optional and required arguments, as well as default values for some optional arguments.
Purely out of curiosity, I am wondering why the word some was included. Is it possible to have an optional argument without a default value?
Upvotes: 8
Views: 13964
Reputation: 32921
*args
and **kwargs
are optional arguments without default values
-- comment by Pranav Hosangadi
Upvotes: 1
Reputation: 8437
In recent versions you can define functions arguments multiple ways. Here is how they can be defined and called:
def fun(a, b, c='foo'):
...
fun(1, 2)
fun(1, 2, 'bar')
fun(1, b=2)
fun(1, b=2, c='bar')
fun(a=1, b=2, c='bar')
where a
and b
are positional or keyword args and c
is an optional positional or keyword arg
or
def fun(a, b, *, c, d='foo'):
...
fun(1, 2, c='bar')
fun(1, b=2, c='bar', d='baz')
fun(a=1, b=2, c='bar', d='baz')
where a
and b
are positional or keyword args and c
is a mandatory kwarg and d is an optional kwarg
or
def fun(a, b, /, c, *, d, e='foo'):
...
fun(1, 2, 3)
fun(1, 2, c=3)
fun(1, 2, c=3, d='bar')
where a
and b
are positional only args, c
is a positional or keyword arg and d
is a mandatory kwarg and e
is an optional kwarg
Upvotes: 5
Reputation: 354
The *
prefix means "arbitrary number of positional parameters", and parameters prefixed by it can be declared without default value.
The word 'some' relates to that reason, you don't have to provide default values for all optional arguments.
Upvotes: 2