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 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.

3 Answers

Josh Keenan
Josh Keenan
19,652 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 68,423 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 68,423 Points

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

The examples in this tutorial are positional arguments. They need to be passed in the order expected by the function.

With keywords you can define the function as such def f(qty, item, price): etc etc etc

and would call function: f(qty=1, item='bananas', price=1.74) so it allows some flexibility in the order for passing arguments.

As explained this is beyond the current level but will probably be made clear as you progress.

Travis Alstrand
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

Also, just like with 'Packing' earlier, the next video is 'Unpacking: A Practical Example'. This is just a simple demonstration to explain the basics of unpacking.