Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functions, Packing, and Unpacking Packing and Unpacking Unpacking

I'm not seeing the utility of packing or unpacking.

It would be nice if these tutorials included a real-world example where packing/unpacking was useful, instead of a passing reference to their usefulness.

2 Answers

Josh Keenan
Josh Keenan
19,426 Points

Well it's hard to put that into perspective, any time you have multiple bits of data being returned from some request can instantly be available as their own variables, a lot of the stuff you learn now won't really seem impressive or relevant for a while. Feel like when I started coding I got overloaded with stuff like this and until i had to make use of it, it was worthless.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

Expanding on Josh's answer, packing and unpacking are done extensively, but it might seem "hidden". In calling any function or method or class instantiation, all arguments are packed into a tuple (for positional arguments) and a dict (for keyword arguments) then they are unpacked as they are assigned to parameters. This will lead to the *arg (unpacking a tuple) and **kwarg (unpacking a dict) idioms you'll learn more about later on.

Packing and unpacking will lead to other useful tricks later on. It's one of the under appreciated features of Python!

Hmmm, this doesn't clarify things much for me. What do you mean by positional vs. keywords arguments? In any event, I'm going to rewatch the entire course. Maybe that'll help me pick up on some of these hidden concepts.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

More on positional and keyword arguments is found in later topics. You’ll get there.