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

Jeffrey Kwok
Jeffrey Kwok
3,006 Points

can someone explain the for loop in the video

for key,value in kwargs.items(): setattr(self, key, value)

I read the teacher notes and under what the setattr function does but I want an understanding of the words used in the for loop.

Kenneth never mentioned kwargs.item before. nor did he specify an "Item" method. Is the "item" method a keyword used to denote the different attributes in the object?

Additionally, if I want to use this for loop in the future, do i have to use the exact words ' for key,value in kwargs.items():'? can the 'key' and 'value' argument only be used in this loop?

2 Answers

Steven Parker
Steven Parker
229,644 Points

The "items()" method just returns each set of keys and values together, so the loop can set both of them.

The names you give to the loop variables is entirely your choice, but "key" and "value" are good choices for names in this case because they refer to what kind of thing each one will hold.

Steven Parker
Steven Parker
229,644 Points

Jeffrey Kwok — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!