Parameters#
A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept.
Find out more in the specific part of the official documentation.
Positional-only#
You can specify arguments that can’t be passed to a function using the syntax <argument name>=<argument value>
. To define such arguments, you can set /
in the list of arguments. All parameters before this symbol can only be passed positionally.
In the following cell, a
and b
are positional-only parameters:
def some_function(a, b, /, c):
print(a, b, c)
The following cell tries to specify the names of the arguments during calls:
some_function(a=10, b=20, c=20)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 some_function(a=10, b=20, c=20)
TypeError: some_function() got some positional-only arguments passed as keyword arguments: 'a, b'
Got the corresponding error message. So you can call the function only by specifying the first and second parameters positionally, as shown in the following code:
some_function(10, 20, c=30)
10 20 30
Keyword-only#
You can define parameters whose values can only be specified through keywords by using *
in the parameter list. All parameters that follow *
must be specified by explicitly providing the parameter name.
The following cell specifies b
and c
arguments as keyword-only.
def some_function(a, *, b, c):
print(a, b, c)
The following cell shows that if you try to pass everything as positional arguments, it results in a corresponding error.
some_function(10, 20, 30)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 1
----> 1 some_function(10, 20, 30)
TypeError: some_function() takes 1 positional argument but 3 were given
But any case where you pass the b
and c
arguments using keyword syntax will work fine.
some_function(10, b=20, c=30)
some_function(c=10, b=20, a=30)
10 20 30
30 20 10
Arguments vs parameters#
Parameters - local variables to which values are assigned at the moment of its call.
Arguments are information passed to the function. They are real values corresponding to the parameters that were specified when the function was declared.
So in following example:
val
is a parameter ofsome_function
;"test"
is an argument passed tosome_function
.
def some_function(val):
print("I got", val)
some_function("test")
I got test