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 trialtomtrnka
9,780 Pointswhy 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?
3 Answers
Steven Parker
231,268 PointsAs 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.
Sergio Andrés Herrera Velásquez
Python Development Techdegree Graduate 12,969 PointsActually, it is a bad practice to pass lists due to their mutability, meaning you could be modifying the list itself, which is somethin that the *args wouldn't cause.
Steven Parker
231,268 PointsIt may not always be bad practice, as sometimes the function might be intended to modify a list.
Sergio Andrés Herrera Velásquez
Python Development Techdegree Graduate 12,969 Pointswell, but as a rule of thumb, what gets modified is a copy of the list, not the list in itself