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 Packing, a Practical Example

tomtrnka
tomtrnka
9,780 Points

why not pass LIST instead of *args

I understand why we used *args, but isn't it better just to pass a LIST as argument? (cause from previous lessons, LIST can contain any amount and type of data too, and its mutable... if we use *args, we always get a tuple). This whole *args and **kwargs is a bit consufing in a way: when to you them.

What happens when the function is defined with *args parameter, and I pass in a list? Will the parameter contain a list nested in a tuple?? Are *args really that common? Dont people 'prepare' the data first in ordered groups and then pass them and process them/use them afterwards?

1 Answer

Steven Parker
Steven Parker
210,837 Points

As I learned Python, I discovered it was not just a language but a style, and one hallmark of "pythonic" style is keeping things simple. It's certainly easier to write (and remember) something like "sum(1, 2, 3)" compared to "sum([1,2,3])".

Of course, nothing prevents you from creating your own functions to work that way if you want. But if you work in a team, the other development team members may have different coding style preferences.