Do all keyword arguments have to written after all the positional arguments?

Question

When writing a function in Python, do we have to place all keyword arguments so that they come after all the positional arguments in the function definition?

Answer

Yes, keyword arguments, which are assigned some value in the definition, must be written after all the positional arguments.

If we do not follow this rule, then Python will give us an error message.

Example code

# This would cause an error, because
# arg1 is a keyword argument but was placed 
# before arg2, a positional argument.
def func(arg1=10, arg2, arg3=30):
  return arg1 + arg2 + arg3

print(func(20))

# All keyword arguments must 
# follow all the positional arguments, so
# this updated function would work.
def fixed_func(arg2, arg1=10, arg3=30):
  return arg1 + arg2 + arg3

print(fixed_func(20)) # This will print 60
30 Likes

4 posts were split to a new topic: Outdated lesson text?

Hey ! Is it possible to put a default argument before a non default argument when defining a function. If yes how can we call it ?

1 Like

Consider,

>>> def remove_x(word, x='i'):
	return ''.join(word.split(x))

>>> remove_x('mississippi')
'msssspp'
>>> def remove_x(x='i', word):
	return ''.join(word.split(x))
SyntaxError: non-default argument follows default argument
>>> 
9 Likes

The syntax error code helped me to understand what they were trying to say in the lesson. Thank you!

2 Likes

So does this imply that default values must always be at the end of a list of arguments? We can’t have it before any non-defaults?

1 Like

More than imply; it’s the rule.

3 Likes

Hi Mtf,
First timer here. I don’t know why I couldn’t seem to post here - still trying to get used to the forum :slight_smile:
Just a simple question, so it says ‘Once you give an argument a default value (making it a keyword argument), no arguments that follow can be used positionally.’ Could I interpret it as keyword/default value argument is not positioned in the same sequence as the print statements? To be more specific, as grocery_store comes ahead of special_item in print statements, then we have to use special_item before grocery_store in the parenthesis after def greet_customer that’s what the web says as well.
However, in the practice, it seems that row_count is defaulted/keyworded and comes after title , which is the same sequence as what it is in the print statement. Does it not seem to be to the contrary?

Would be good to know why.
Thanks,
Jane

The order in which we print items is arbitrary (as in doesn’t matter). However, the order in which we position the arguments in a function call definitely matters.

The statement in your post describes where we can position default parameters in the function’s parameter list. They cannot be followed by any other positional arguments.

 func(arg1, arg2)        # a call with two positional arguments

 def func(a, b, c=0):    # a signature line with a third optional parameter

The default parameters must always be last.

Thanks so much for your prompt response!

I see. Just to confirm, as the order of how we input the parameters in the print statements doesn’t matter, I assume the order in the function parenthesis has nothing to do with that and the no.1 rule is that default must be left as the last, not followed by any positional argument.
Thanks,
Jane

1 Like

The order of the arguments is important so that we are passing the values to the correct parameter.

a => receives arg1
b => receives arg2

Those two arguments must be present in the function call or an exception will be raised. We do not need to third argument since it has a default value already set. Should we include a third argument, say, arg3, then

c => arg3

Now if we wish to print these values in the function, we can choose any order…

print (c, b, a)

would be just fine. The print function doesn’t have any positional arguments, and can accept any number of comma separated expressions, or any manner of formatting. That function does nothing but convey the expressions to the standard output (usually the terminal).

1 Like

I will always commend mtf for the wonderful job you do in ensuring that we comprehend the topic. After completing the Function class, i am now on revision of the subject matter and i can see the frustration of students here. I felt exactly same way and it took me time to figure it out.

I just feel (in my opinion which might be wrong) that Codecademy should try to simplify choice of sentence especially if it causes confusion here and there just like the statement "When a function has a default value for one of its parameters, we can no longer call it positionally, and only by keyword.” and “Once you give an argument a default value (making it a keyword argument), no arguments that follow can be used positionally.” What about if it was written as “Keyword Arguments should always come last” ? Or a better or more easily comprehensible way to put it.

I must also mention that it might be in the wisdom of CodeAcademy to make us (programming students) think deeply as part of the training

7 Likes

Ambiguous instructional material is a bug, not feature! There are plenty of ways to encourage thinking and plenty of resources to which students could be directed from within the instructional framework if only those entrusted with curriculum creation were also obligated to keep up with the forum and do regular maintenance on the courses.

But thats the difference between an online course and an online class. With the latter, you get regular access to the professsor, and daily access to a grad student/assistant. You pay $600-$1800 dollars and get actual college or university degree credit. With the former, you get pre-taped lectures or readings, which may or may not be updated as needed, access to people who do this as a hobby, pay a few hundred dollars (or nothing, to learn even more outdated material), and might get a “certificate.”

3 Likes

I understand that keyword arguments are positioned last in the arguments, but I haven’t found anything online about why they are last. Does anyone here have any idea? Does it have to do with Python assigning to keyword argument’s values to positional arguments if it were not last?

1 Like

Very Nice explanation…crystal clear

In Python, the convention of placing keyword arguments after positional arguments is a design choice that simplifies the syntax and enhances readability.

  • Placing positional arguments first and keyword arguments last makes function calls more readable.
  • When reading the function call, you can quickly identify the positional arguments without having to explicitly specify the parameter names.
  • The consistency in the order of arguments simplifies the understanding of function calls.
  • It becomes easier to predict where to find positional and keyword arguments when looking at function signatures.
  • Placing keyword arguments last avoids potential ambiguity when interpreting function calls.