FAQ: Learn Python: Function Arguments - Using Both Keyword and Positional Unpacking

This community-built FAQ covers the “Using Both Keyword and Positional Unpacking” exercise from the lesson “Learn Python: Function Arguments”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Using Both Keyword and Positional Unpacking

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Confused on part 12. It says calls must be made in this order:

  • All named positional parameters
  • An unpacked positional parameter ( *args )
  • All named keyword parameters
  • An unpacked keyword parameter ( **kwargs )

but in the example, the **kwargs call happens 3rd, and then the keyword parameter comes 4th:


Am I going crazy or is this a mistake?

@chip2653472605, Good pick - up!

As it turns out, the named (i.e., default) keyword argument and the **kwargs arguments can be interchanged in the function call. The only hard-and-fast rule is positional before named. But the interpreter is smart enough to sort out the named parameter from the keys in the **kwargs parameters.

You can play around with this in the accompanying exercise, where the default provided function header is this:
def remove(filename, *args, **kwargs):
… and the provided function call is this:
print(remove("text.txt", "generous", "gallant", fond="amused by", Robin="Mr. Robin"))

Once you get the function running, to the suggested parameters in the exercise, add this one title = "Robin Hood"
… and place it in the function definition just before **kwargs:, like this:
def remove(filename, *args, title = "Robin Hood", **kwargs):
… and then immediately before the return statement in the function body, put
text = title + "\n" + text

Now, when you run the function, the printed text will be headed by the title, Robin Hood

So, here you go: Change the function call to this

  • print(remove(“text.txt”, “generous”, “gallant”, title = “New Story”, fond=“amused by”, Robin=“Mr. Robin”))

… or this

  • print(remove(“text.txt”, “generous”, “gallant”, fond=“amused by”, title = “New Story”, Robin=“Mr. Robin”))

… or this:

  • print(remove(“text.txt”, “generous”, “gallant”, fond=“amused by”, Robin=“Mr. Robin”, title = “New Story” ))

… and it will work with the named keyword parameter before, after, or in between the **kwargs!