Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Method Arguments

Wesley Trayer
Wesley Trayer
13,812 Points

"self.name = name" assigns "name" a boolean when I have entered a string.

My code:

import random

class Thief:
  sneaky = True

  def __init__(self, name, sneaky=True, **kwargs):
    self.name = name
    self.name = sneaky

    for key, value in kwargs.items():
      setattr(self, key, value)

  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

In the console, when I make a new instance of the class, I enter the required "name" argument, but when I try to check it, it returns "True".

>>> from characters import Thief
>>> me = Thief("AlfredTheUnsneaky")
>>> me.name
True       # This is where the confusion is.
>>> me.name = "SneakierYet"
>>> me.name
"SneakierYet"

I assume the problem lies in the "__init__", but I can't find a difference between my code and Kenneth's.

Thanks for any help!

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hmmm

self.name = sneaky

Seems like a sneaky bug ;)

Let me know if that hint doesn't do the trick.

Wesley Trayer
Wesley Trayer
13,812 Points

Thank you Craig! I should have caught that.

Craig Dennis
Craig Dennis
Treehouse Teacher

Only could spot it because I've made that mistake a ton ;)