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 trial
mourad marzouk
5,560 Points=None in the parameters
Is this video he set the parameter to unpacker variables as =None. So how come when he passed passed it through that in unpacker is took the variables but when he passed it through packer it didn't? is it because its a dictionary? and if so then why have the variables=None at all?
https://teamtreehouse.com/library/packing-and-unpacking-dictionaries
2 Answers
Greg Kaleka
39,021 PointsGood question Mourad. This is tricky.
Short answer is that it did take the variables both times. It also has nothing to do with the =None, which is just a default value. Kenneth just didn't use the name variable in the packer example. He was trying to show that it doesn't get picked up in the **kwargs because it's already been taken by the name variable.
Long answer:
In the packer example, at one point he defines the function with name=None as one parameter. This means you can pass a name to the function, or not, in which case it defaults to None. The defaulting to None doesn't really matter here, though. What's happening in the example is just that name is captured as a local variable in the function, and **kwargs is a separate dict variable that catches any other kwargs passed to the function. By explicitly defining name as a parameter, we can access it in the function's code.
That means we could have expanded the function like this:
def packer(name=None, **kwargs):
print(kwargs)
print(name)
And the output of calling packer(name="Kenneth", num=42, spanish_inquisition=None) (see 3:00 in the video) would be:
{'num': 42, 'spanish_inquisition': None}
Kenneth
In unpacker, we again have None as default values, which don't get used unless the function is called without them. In the example, he does use both the first_name and last_name variables, which is why they show up.
Hope that's not too confusing. I really strongly encourage you to jump into a workspace and try these out. Call the function a few different ways. Edit the function to use the various parameters.
Happy coding!
Cheers
-Greg
mourad marzouk
5,560 PointsYes, thank you Greg. It's all clear now. Thank you for the awesome explanation. I understood it right away.