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 (retired) Objects __init__

Michael Kelly
Michael Kelly
4,965 Points

Am I misunderstanding the directions?

The instructions in the second step tell me to pass in a name set to any default value I choose and then set self.name to name. However, when I do this it says step 1 is no longer working. Step 1 is to set name to my name.

It seems I can't do both in one class as they are contradictory instructions. What am I missing?

2 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

The second task is asking for you to override the init method so you can assign values to your member variables. This method is some what similar to the constructors in Java. You could do it like this:

class Student:
    name = "Federico"

    def __init__(self, name="Anonymous"):
        self.name = name;

I hope that helps a little bit.

Michael Kelly
Michael Kelly
4,965 Points

Hi Carlos,

Thank you for the feedback. It turns out I forgot to put def before init so it told me Task 1 no longer works. I thought it was a conflict because I was assigning another value to name.

Your code helped me catch the error. Thanks!

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

That's right, you have to put the keyword "def" before the name init so it understands that it's a method that we are talking about.