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!
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

Ewerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsInstead of using setattr, I tried doing this and it didn't work. Why?
def __init__(self, name, sneaky = True, **kwargs):
self.name = name
self.sneaky = sneaky
for key, value in kwargs.items():
self.key = value
The code was compiled ok. Then, when I declare the object like:
ewerton = Thief("Ewerton", age = 28, hobby = "steal")
The code runs ok too. But if when I try to check some of the object's attribute:
ewerton.age
I get this error message:
Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'Thief' object has no attribute 'age'
Why, instead of using setattr, can't I use a for loop to set values to the object as I would do in other situations?
1 Answer

Dave StSomeWhere
19,870 PointsWhen setting self.key - the variable key is not being resolved, it is creating an attribute called "key" - not what you want and why we need to use setattr
.
ewerton = Thief("Ewerton", age = 28, hobby = "steal")
ewerton.key # check your attribute called key - should = "steal"
Ewerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsEwerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsOh... I got it! Very much appreciated!!!