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
Why does Kenneth pass in the *args and **kwargs if we don't need them?
What's the point of passing in the *args and **kwargs if we don't need them or even as he said he couldn't think of a reason to use them?
Ari Misha19,323 Points
Hiya Michael! You can always use "setattr" method and assign 'em values. Here is the code for using setattr on your kwargs:
for keys, values in kwargs.items():
setattr(self, keys, value)
Put this code in your init method in order to initialize the object with any no. of arguments. Also remember the zen of Python or guidelines for writing Python codes better , "explicit is better than implicit". So yeah being explicit is considered the best practice in Python.
I hope it helped. (:
I think he uses *args and **kwargs even in the case we're not using them because, otherwise, if the user of the code passes any not needed argument it would cause an TypeError saying that he exceeded the number of arguments of the class.
But we solve this problem using args and kwargs as parameters because the code runs without error for whatever number of arguments passed to the class, since they will all be caught by *args and **kwargs. Since we won't use them, we don't pass these arguments further.
That is my interpretation of this. I'm not entirely sure if I'm right though.
Hope it helped you!