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

Python Object-Oriented Python Instant Objects Methods

What 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
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good 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
Euenlee Tan
2,165 Points

When 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
Chris Freeman
Treehouse Moderator 68,457 Points

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