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

What Does this code Do?

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

Would you guys explain what does this code do? Totally lost?

1 Answer

Steven Parker
Steven Parker
229,670 Points

When you create a new "Thief" object, this method runs and sets the starting conditions of the internal variables "name" and "sneaky".

The "name" must be provided when the object is created, and the "sneaky" value is optional. the "sneaky=True" code will set it to "True" by default if you leave it off. Examples:

lefty = Thief("Ralph", False)  # new Thief "lefty", name is "Ralph" and not sneaky
digger = Thief("Joe")          # new Thief "digger", name is "Joe" and IS sneaky