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

Not understanding the question

I don't know what's wrong it just keeps saying 'try again'

class Student:
  name = 'edward'

  def __init__(self, name):
    self.name = 'eddy'

1 Answer

Simon Merrick
Simon Merrick
18,305 Points

You haven't stated what the question actually is, though i think you are running into issues because of the way you have declared your variables.

The python documentation uses this example to show how to declare both class and instance variables

class Dog:
    kind = 'canine'                    # class variable shared by all instances
    def __init__(self, name):
        self.name = name           # instance variable unique to each instance

In your example you are setting self.name to 'eddy' every time. If you want to set the name variable for each instance you need something like this

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

# Then you can create a new instance of Student, named Eddy, like this
# student = Student('Eddy')
# print(student.name)
# >> Eddy