Confused on part 12. It says calls must be made in this order:
- All named positional parameters
- An unpacked positional parameter (
- All named keyword parameters
- An unpacked keyword parameter (
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,
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!