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
Jake Du
2,369 PointsChanging the attribute of a class directly
In the video, kenneth showed us that you can change the attribute of an instance hence "kenneth.sneaky = False" and showed us that this does not affect the class attribute and "Thief.sneaky" remained unchanged.
However; he also mentioned that this may not be the case all the time. I tried changing the attribute of the class directly "Thief.sneaky = False" and I was assuming that it would also apply to "kenneth.sneaky = False" but it didnt'. Can someone please expound on this further? I assumed that changing the attribute of the class itself would change the attribute of all copies of the class.
1 Answer
Steven Parker
243,658 PointsIt's the difference between a class variable and an instance variable. If an instance variable exists, it will override a class variable of the same name. But if only the class variable exists, it can be access through any instance of the class.