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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Artur Owczarek](https://secure.gravatar.com/avatar/62523a54cbbce450ef50b3461691cbbc?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Artur Owczarek
4,781 PointsWhy Kenneth in the beginning of class initiates attribute?
The question as in the title. This is my files of attributes, thieves, character:
import random
class Sneaky:
def __init__(self, sneaky=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sneaky = sneaky
def hide(self, light_level):
return self.sneaky and light_level < 10
class Agile:
def __init__(self, agile=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.agile = agile
def evade(self):
return self.agile and random.randint(0, 1)
import random
from characters import Character
from attributes import Agile, Sneaky
class Thief(Sneaky, Agile, Character):
def pickpocket(self):
return self.sneaky and bool(random.randint(0, 1))
class Character:
def __init__(self, name='', **kwargs):
self.name = name
for key, value in kwargs.items():
setattr(self, key, value)
Sorry for format, but I can't see options for formatting. Anyway, I didn't it what Kenneth did and it works all fine. I mean he writes agile = True in the beginning Agile class, I don't know why.
2 Answers
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 PointsGood Question. You are correct that, since the class instances set the attributes during instance initialization, they do not also need attributes to be set in the class.
One situation that would warrant setting in both the class and in __init__
is if a classmethod relied on the attribute. When running classmethods there is no instance and hence no self
available.
![Than Win Hline](https://secure.gravatar.com/avatar/f47f2360b9e97250eee7fa2519617c90?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Than Win Hline
1,679 PointsI'm taking the oop course again to understand more. Can you please explain how can we use super() in parent class in attributes.py. I thought super() is used only in the subclass to call parent class init method. I tried to run the code in Jupyter notebook, I get en error which says that 'init' takes one exactly argument which makes sense.
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 Pointssuper()
can be used within any class method to call the first method of the same name found in the listed parent classes along the MRO.
If super()
is found again in the parent class, the next method of the same name found in the MRO is run. This will continue until no more super()
calls are found.
Be sure you know which __init__
the error refers to. The stacktrace will show the line number.
Post back if you need more help. Good luck!!!