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 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 ;)