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 Object-Oriented Python (retired) Hack-n-Slash Last Steps For Character

When do I have to use **kwargs in an __init__ or __str__ and when not?

Hey guys, can one of you just point briefly out, when it is recommended to use **kwargs at an instance and when not?

At the exercises I always trying to do the next steps on my own before I watch the video and there I noticed that I struggle with it, when to use **kwargs and not.

Thanks for your help, Cheers Urs

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

**kwargs is used when do not know what keyword arguments will be passed to a function or method. Like its positional argument cousin **args, they are both the "wild card" way to define parameters.

If you can use named variables and named variables-with-default values explicitly, then you don't (and probably shouldn't) use the wildcard parameters **kwargs or **args.

If used **kwargs must be the last parameter.

Comment if you have more questions.

Thanks Chris that helped me :-)