An Explanation in Keyword Argument Section Confuses Me

Hi everybody!

I read this explanation about Keyword Arguments and I’m confused by it.

image
What is that supposed to mean? Are that two def commands in the different scenario? Or it pictures twice declarations? Why is defining special_item first is not valid?

Thank you very much,

Abdul Mubdi

HI @abdulmbindar.

It looks like you have two different terms confused. The function definitions you have shown are to demonstatre default arguments (these don’t have to be specified when calling the function, they have a default value).

def func(name, age=10):
    return f"My name is {name} and my age is {age}"


print(func("Barry"))  # relies on the default age, which is 10
Out: My name is Barry and my age is 10
print(func("Barry", age=100))  # can be called with a keyword argument
Out: My name is Barry and my age is 100
print(func("Barry", 200))  # a keyword argument isn't necessary though
Out: My name is Barry and my age is 200  # it will default to the order the names are defined


def func(age=10, name):  # This will throw an error, defaults cannot come before positionals.
    return

Two functions are used to show that positional arguments must come ahead of default arguments. The first one will throw an error. This is to avoid potential confusion when calling the function.

As for keyword arguments-
If you called a function with a named argument, that would be a keyword argument, e.g.

def func(a, b):
    return a * b


func(1, b=3)  # b=3 would be the keyword argument.
func(b=3, a=1)  # two keyword arguments here, we can ignore order when using keyword arguments
func(a=1, 3)  # will throw an error because a keyword argument a=3, appears before a positional argument
func(c=1, b=3)  # will throw an error as c is not a valid argument name

It’s’s just an argument to a function call where the name is assigned.

Keywrod arguments can be confused with the arbitrary keyword arguments signified by ** in the function definition but you’ll probably come across this later and you can ignore it for now. It’s the terminology rather than their use which is a bit iffy.

4 Likes

Hi @tgrtim

Thank you for answering my question quickly. Apparently it took me a day to understand it. :grin:

So now that I feel like I get it, I want to rephrase those explanation to make sure that I indeed do:

Python won’t let you type a default argument at the beginning of parameter assignment. If you want to specify one or more of the parameter to have a default value, make sure you place it at least at the second position unless you want all the parameters to have a default value.

Did I say that correctly?

Thank you very much

2 Likes

Edit: see this FAQ answer for more detail.

Rather than second position or at the beginning it’s more like any default arguments must come after all positional arguments when defining the function.

def func(a=3, b=1):  # default at the beginning is fine
    pass
def func(a, b, c, d=10, e, f=range(100)):
    pass  # throws an error because a default (d) comes before a positional (e)
2 Likes

Alright, thank you so much!

1 Like