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

Why would you not just use a list argument instead of using *args? What is the benefit of using *args?

What usecases are there where using *args is better than requesting or expecting a list parameter?

2 Answers

Steven Parker
Steven Parker
229,732 Points

Packing comes in handy when you are implementing or modifying an interface that is already designed to take multiple arguments, or you are creating something new and want to provide a more compact calling sequence for the user.

So the benefit is just readability? By "provide a more compact calling sequence", I assume you mean that *args allows the user to just create the variables inline rather than having to create a list first and then pass that into the method call

Movies('Titanic', 'Inception')

vs

movies = ['Titanic', 'Inception'] Movies(movies)

Steven Parker
Steven Parker
229,732 Points

Or,   Movies('Titanic', 'Inception')   vs.   Movies(['Titanic', 'Inception']) :point_left: extra brackets.

Hope that helped. You can mark the question solved by choosing a "best answer".
And happy coding!