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__

_init_ and self

I have tried this a million ways, but don't understand what I'm doing wrong.

student.py
class Student:
  name="Katy"

  def _init_(self):
    self.name="Sally"
    return name

1 Answer

Drew Butcher
Drew Butcher
33,160 Points

I think the problem is that you are using the return in your init, you don't need to.
Also, you need to use two underline marks before and after the init.
Also, i don't think you need to define name outside of the init and inside the init: I would try something like the following:

class Student():
    def __init__(self, name="Kathy"):
        self.name=name

Now in the terminal you can cd into the folder containing this file( say the files name is 'student.py') and then type 'python' to get the interactive python shell running and import your class by typing 'from filename import Student' (here filename is whatever you called the file without the .py at the end so if you called the file 'student.py' then you would use 'from student import Student') once you run this command you will have access to your student class. So you can create a new student by typing 'student_one = Student(name='Sally')' to check student_one's name you would type 'student_one.name' and the terminal should return 'Sally'. However, if you type 'student_two = Student()' then you should get a student whose name is 'Kathy'.

I hope this helps.