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
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
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?
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
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
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).
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
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.â
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?
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.