Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSarah Toukan
1,079 PointsUnderstanding use of the double asterisks (**)
In the Packing and Unpacking lecture, Kenneth defines the following function:
def unpacker(first_name=None, last_name=None):
if first_name and last_name:
print("Hi {} {}!".format(first_name, last_name))
else:
print("Hi no name!")
and then he uses this to call it:
unpacker(**{"last_name": "Love", "first_name":"Kenneth"})
Firstly, I'm trying to understand the reasoning behind the way he calls the function. If I simply try
unpacker("Kenneth", "Love")
it gives me the same result. So what benefit is there with the longer version?
Secondly I thought the purpose of using the double asterisks was to allow a variable number of arguments, but when I try I'm not allowed to pass in a dictionary with anything other than "last_name", and "first_name".
1 Answer
Steven Parker
229,204 PointsYou didn't provide a link to a course page, but I'd guess that this unusual argument passing is simply done as a demonstration of how keyword arguments don't have to be provided any any particular order. Your use of positional arguments is much more likely to be the way that function would be called in actual practice.
And this function is defined to specifically take only those two arguments, so that would explain getting an error if anything other (or additional) were supplied.
Sarah Toukan
1,079 PointsSarah Toukan
1,079 PointsThank you Steven, I'll edit the post to add the link.
I still don't totally understand the use of the double asterisks when calling the function. If I remove them but write the arguments in the correct order:
unpacker({"first_name": "Kenneth", "last_name":"Love"})
the resulting output is this:
Hi no name!
It's as if I passed nothing into the function?
Sarah Toukan
1,079 PointsSarah Toukan
1,079 PointsIf I remove the quotes I get a NameError
Steven Parker
229,204 PointsSteven Parker
229,204 PointsYou also need to remove the braces and convert the :'s to ='s:
unpacker(first_name="Kenneth", last_name="Love") # the unpacked equivalent
The function is expecting individual string arguments, but without the "**" operator to unpack the dictionary, you're just passing it a dictionary as a single argument (which is ignored).
Sarah Toukan
1,079 PointsSarah Toukan
1,079 PointsThank you Steven, the explanation is very helpful
Steven Parker
229,204 PointsSteven Parker
229,204 PointsSarah Toukan — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!