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) Inheritance Intro to Inheritance

How does a new attribute like adjective='manxsome' become a kwargs dictionary (or its item)?

Is it all about those 2 stars (**), which make all the magic? Or all that those 2 stars do is show that we have a dictionary here?

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Let's use this function as example:

def add(a, b):
  return a + b

There're two ways you can call this function.

First way is by passing in positional arguments.

print(add(3,4))

Second way, by keyword arguments.

print(add(b=4, a=3))

(notice that by passing keyword arguments, you are free to place them out of order). For the add function, it takes EXACTLY 2 arguments, passing in more or less arguments and you'll get an ArugmentError runtime exception.

Sometimes it's useful to write a function that accept any numbers of arguments.

def foo(*args, **kwargs):
  for i in args: print(i)
  for key, value in kwargs.items(): print(key, value)

In this function definition, There's no required arguments, but there're *args, and **kwargs here. I can call this function by supply any numbers of arguments, whether they are keyword argument or positional arguments.

foo(3, 4, 9, "yo", keyword1=3, keyword2="hello")

So when I call the function like above, 3, 4, 9, "yo" as positional arguments, they get stored in args tuples, and keyword1=3, keyword2="hello" as keyword argument get stored in kwargs dictionary, so that you can do something about them in the function body. (In this foo case, I just print them all out).

Hope that helps.

sidni ahmed
sidni ahmed
3,329 Points

Thanks William Li, you really made it sound so simple. Love the way you broke it down.