Day 2 of diving into Python functions — today we’ll look at:

positional arguments
named (or keyword) arguments

I don’t have a preference on which term to use for the latter!

Here’s a simple function – see attached image

—> Which of the five function calls will not work?


While you think about the answer, you can refresh your memory about the terms with yesterdays toot thread!

The parameters in the code above are person and number

The arguments are the strings with names and the integers used in the function calls

/1

Let’s look at all five function calls in this example:

– 1 –

greet_person("Elizabeth", 5)

This is the most commonly used function call. The arguments are positional arguments

This means that the values "Elizabeth" and 5 are matched to the parameter names person and number depending on the their position in the function call

The first argument is assigned to the first parameter; the second argument to the second parameter…

/2

Show thread

– 2 –

greet_person("Ishaan", number=3)

In the second call, the first argument, "Ishaan", is a positional argument as in the first example

However, the second argument is a named argument or a keyword argument

The argument is matched to the parameter by naming it. You’re using the parameter name with an equals before the argument in the function call

Therefore, in this second example, you have one positional argument and one keyword (or named) argument

/3

Show thread

– 3 –

greet_person(person="Stephen", 10)

This seems identical to the case in the second example, but we come across one of the rules when using positional and keyword parameters

See the description of the SyntaxError. It says positional argument follows keyword argument

When using a mixture of positional and keyword arguments, the positional arguments must come first

And this makes perfect sense, since Python is relying on the position of these arguments within the function call to know which parameter name to assign them to

/4

Show thread

– 4 –

greet_person(person="Stephen", number=10)

In this case, you’ve used both arguments as named or keyword arguments. You’re no longer relying on the position of the arguments. What matters now is the keyword you use when calling the function.

This leads us nicely to number 5…

/5

Show thread

– 5 –

greet_person(number=10, person="Stephen”)

Since you’re using these arguments as named arguments, you no longer need to stick to the order in which they’re defined in the function signature

Python no longer uses position to assign the arguments to the parameter names. This is particularly useful in functions which can take many parameters

In this example, the programmer calling the greet_person() function has a choice on whether to use positional arguments, named arguments, or a mixture of both (as long as the positional arguments come before the named ones)

There are ways in which the programmer who defines the function can force the user to use positional-only or keyword-only arguments

But we’ll leave that discussion for another day…

/6

Show thread

Summary: positional arguments and named (keyword) arguments

In summary, arguments can be positional arguments or named (keyword) arguments

When using positional arguments, the arguments are matched to parameter names depending on their position

Named (keyword) arguments include the parameter name in the function call

Tomorrow, we’ll look at optional arguments which have a default value…

/7

Show thread

In case you want to go back to Day 1 of this series on Intermediate Python functions, here’s the link: qoto.org/@s_gruppetta/10930187

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.