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 trialBrian Rodriguez
2,811 PointsWhy the use of *args in multiple-superclasses video?
Can you explain why you used *args
in this video for the Agile and Sneaky classes? What does this do, and what value does it add above and beyond using **kwargs
?
[MOD: edited to escape use of asterisks. -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsThe *args
and **kwargs
become catch-all bins for regular arguments and keyword arguments, respectively. Since the additional Agile
and Sneaky
class are unaware of what other arguments that might be passed to other classes in the Method Resolution Order chain, the safe approach is to use these catch-all parameters to hold all arguments, then pass on the all of these arguments to the next Class on the chain.
In an extended example, what would happen if a Agile
was used in creating other classes that were based on, say, Monster
or Politician
or Animal
? The Agile
class has no idea what arguments are being passed in and which, if any, of the classes further down the MRO chain need which parameters. Using *args
and **kwargs
provides a convenient why to pass arguments along.
As an aside, it is the single-asterisk and double-asterisk that provides the functionality, not the "args" and "kwargs" text. The args and kwargs have been chosen as a recognizable convention, much like using "self" for class methods. It could just as easily been *things
and **wordthings
(and thismethod
).
See more at python.org docs 3 tutorial controlflow keyword-arguments