21. TLDR regarding function arguments π‘#
Till now we have seen all the 4 types of arguments that we can use in functions.
Positional Arguments
Unnamed positional Arguments / VarArgs
Keyword-only Arguments
Keyword arguments / Varkwargs
Letβs give it a shot with all the above arguments in a function π
The complete syntax of a function eliding varargs and keyword arguments defined in PEP-570 would be as:
def name(positional_only_parameters, /, positional_or_keyword_parameters, *, keyword_only_parameters):
Example:
Letβs build a complete function with all the types of arguments π.
def function(positional_only, /, position="π", *varargs, keyword_only, **keyword):
print(f"{positional_only=}")
print(f"{position=}")
print(f"{varargs=}")
print(f"{keyword_only=}")
print(f"{keyword=}")
# datatype of varargs and keyword.
print(f"The datatype of varargs is {type(varargs)}")
print(f"The datatype of keyword is {type(keyword)}")
Letβs call our beautiful function β₯οΈ
function(
"Python",
"β₯οΈ",
"Python",
"is",
"Cool",
keyword_only="π",
key1="value1",
key2="value2",
)
positional_only='Python'
position='β₯οΈ'
varargs=('Python', 'is', 'Cool')
keyword_only='π'
keyword={'key1': 'value1', 'key2': 'value2'}
The datatype of varargs is <class 'tuple'>
The datatype of keyword is <class 'dict'>
The above calling of function can also be written as:
function(
"Python",
"β₯οΈ",
*["Python", "is", "Cool"],
keyword_only="π",
**{"key1": "value1", "key2": "value2"},
) # Unpacking.
positional_only='Python'
position='β₯οΈ'
varargs=('Python', 'is', 'Cool')
keyword_only='π'
keyword={'key1': 'value1', 'key2': 'value2'}
The datatype of varargs is <class 'tuple'>
The datatype of keyword is <class 'dict'>
Letβs make position
be itβs default value.
function(
"Python",
*["Python", "is", "Cool"],
keyword_only="π",
**{"key1": "value1", "key2": "value2"},
)
positional_only='Python'
position='Python'
varargs=('is', 'Cool')
keyword_only='π'
keyword={'key1': 'value1', 'key2': 'value2'}
The datatype of varargs is <class 'tuple'>
The datatype of keyword is <class 'dict'>
Ouch, we see that position
has taken the Python
as itβs value which we intended to be one of the value of our varargs
. The solution for this is to pass our *["Python", "is", "Cool"]
as keyword argument like varargs=["Python", "is", "Cool"]
. NOTE that there wonβt be unpacking symbol *
here.
function(
"Python",
varargs=["Python", "is", "Cool"],
keyword_only="π",
**{"key1": "value1", "key2": "value2"},
)
positional_only='Python'
position='π'
varargs=()
keyword_only='π'
keyword={'varargs': ['Python', 'is', 'Cool'], 'key1': 'value1', 'key2': 'value2'}
The datatype of varargs is <class 'tuple'>
The datatype of keyword is <class 'dict'>
We can even notice that in the above example, we have passed varargs=["Python", "is", "Cool"]
, but in the output the datatype of varargs is printed as tuple. Not just in above example, in all the above examples, we can see that varargs
is tuple
and keyword
is dictionary
.
π‘ Unnamed Positional arguments datatype is always tuple and keyword argument datatype is always dictionary .