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)
![Hanna Han](https://secure.gravatar.com/avatar/b0697df9d388fb50841501a9ab640e75?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Hanna Han
4,105 PointsWhat does "if self.sneaky:" mean exactly?
Hi,
I don't get "if self.sneaky" part in the following code. What value does self.snekay get/have in running the method? I assume self.sneaky value is True, right? Then if self.sneaky is True then randomly it can be called True and False by bool(random.randint(0,1)). If bool is called True then it is True, if bool is False then False...That part I under stand but why it should be run by bool in if condition? I can be just simple self.sneaky is True or not. I don't just get this logic in the code the course taken.
import random
class Thief:
sneaky = True
def pickpocket(self):
if self.sneaky:
return bool(random.randint(0, 1))
return False
2 Answers
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 PointsGood question! Here's how I read the following code:
import random
class Thief:
sneaky = True # class attribute (will exist in all class instances)
def pickpocket(self):
"""method to determine if thief successfully picks a pocket
based on sneaky attribute and a bit of luck.
"""
# if attribute is True, use chance to see if successful
if self.sneaky:
# return 50-50 chance on being successful
return bool(random.randint(0, 1))
# if not returned above, self.sneaky must be false.
# so no chance at success. return False (failure)
return False
In this code segment, self.sneaky
will always be True
so the if
will always run. In later examples, a sneaky
value will be passed into the __init__()
method where it will be assigned to self.sneaky
thus making the if
check in the pickpocket
method necessary.
Post back if you have more questions. Good Luck!
![Euenlee Tan](https://uploads.teamtreehouse.com/production/profile-photos/5080762/micro_Euenlee.jpg)
Euenlee Tan
2,165 PointsWhen Kenneth says, kenneth.sneaky = False, what does this exact line of code mean? Does it mean he assigns False to Kenneth? Also, in the line, if self.sneaky:, shouldn't it be if self.sneaky = True: ? I totally do not understand the last few parts of this video of what Kenneth is actually doing
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 PointsWith the line
kenneth = Thief()
kenneth
is an instance of Thief
with all of the same attributes: name, sneaky, agile, etc. The line
kenneth.sneaky = False
sets the attribute sneaky
to False. In reality, attributes are just labels that point to objects. In this case, the label is set to point to the built-in object False
.
The three lines below behave the same
# is the object pointed at by the label sneaky
# equivalent to the built-in `True` object?
if self.sneaky == True:
# is the object pointed at by the label sneaky
# the same object as the built-in `True` object?
if self.sneaky is True:
# does the object pointed at by the label
# sneaky have a “truthy” value?
if self.sneaky:
a truthy value is a non-zero, or non-empty list, set, dict, tuple, etc.
Post back if you need more help. Good luck!!!