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

Python Python Collections (2016, retired 2019) Dictionaries Packing and Unpacking Dictionaries

Konrad Hunter
Konrad Hunter
3,629 Points

Why do you set the variable to None?

here's the code in question. 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!") What does setting the variable to none accomplish? I rewrote it to exclude the '= None' and it did the same thing.

4 Answers

Here we are defaulting the inputs as None. You can override this when calling the function, but if you do not override the arguments, you will receive back "Hi no name!"

Tane Martin
Tane Martin
2,348 Points

Hey Adam, in that situation, it was set up like this

def packer(name = None, **kwargs):
    print(kwargs)
packer(name = 'kenneth', num = 42, spi = None)

You're right - he did override the name, but the print function was only setup to print the kwargs, not name

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

I'm confused though - Kenneth appears to override name=None when he calls packer(), but at about 3:14 in the video, but the function doesn't return 'name'. Why is this?

Michael Neimat
Michael Neimat
2,680 Points

I think he did this to show you that **kwargs is acting like a catch all. anything that was not defined was caught and put in the dictionary.