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 trialTibor Ruzinyi
17,968 PointsLight level no self ?
Hello all, iam a bit confused about the self attribute. Why whe use self.sneaky but only light_level without the self ?
import random
class Thief:
sneaky = True
def pickpocket(self):
if self.sneaky:
return bool(random.randint(0, 1))
return False
def hide(self,light_level):
return self.sneaky and light_level < 10
1 Answer
Ronald Kratochwill
11,213 Pointsthe word self is only used to call attributes or other methods of the class. In this class, the only attribute is sneaky which is declared in the body of the class. Light_level is not an attribute. It is simply a parameter that becomes an argument of the hide function. You do not need the self because you are using an argument that was put into the function.