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 Your first class

Ashley Keeling
Ashley Keeling
11,476 Points

I don't get instances(class challenge)

I have looked at the teacher notes and I have tried to do but it wont work

first_class.py
class Student:
    name="Ashley"
    me.student(name)
    print(me)

5 Answers

Jakob Hansen
Jakob Hansen
13,746 Points

Okay I did the challenge myself now, and it should look like this:

class Student:
        name = "Ashley"

me = Student()
print(me.name)
Jakob Hansen
Jakob Hansen
13,746 Points

Apparently the treehouse challenge, wants you to split it across 2 lines, i thought it was simpler to just group it. We just create an instance of Student, and then first acces the name attribute when printing it to the console.

Ashley Keeling
Ashley Keeling
11,476 Points

THANK YOU I passed the challenge, thanks for all of the help

Jakob Hansen
Jakob Hansen
13,746 Points

Think of a class as a blueprint or a recipe, it's not the actual cake, you can't eat the recipe ;).

You have to instantiate the class, to use an instance of it. To create an instance of a class, you would do this considering you had a class called Student

class Student: name = "Ashley"

me = Student.name print(me)

You use dot notation to acces properties inside the class :) dot notation being "Student.name"

Ashley Keeling
Ashley Keeling
11,476 Points

I still don't get what I have done wrong, what am I missing in the code

AJ Salmon
AJ Salmon
5,675 Points

This is a really good analogy!

Jakob Hansen
Jakob Hansen
13,746 Points

Alright, so we can give a class attributes, in this case, name is an attribute, we assign it a default value ( your name in this case)

class Student:
     name = "Ashley"

then to instantiate the class, we first need to create a variable to hold this instance of the class. you have got the right idea, in your code, but the indentation is making Python think that

    me.student(name)
    print(me)

is meant to be inside the class declaration, which it's not.

so, the problem is indentation, and then using dot notation to acces the .name attribute of your Student class, instead of student(name) :) I hope this helps

Ashley Keeling
Ashley Keeling
11,476 Points

i have done this and it is saying ,task 1 is no longer passing :

class Student:

name="Ashley"

me=student.name

print(me)

Jakob Hansen
Jakob Hansen
13,746 Points

That's because

class Student:

name="Ashley"

name = "Ashley" is no longer indented. Which means it's not part of the Student class declaration.

it's supposed to look like this:

class Student:

        name="Ashley" #This being indented, so Python know's it is INSIDE the class declaration

me=student().name

print(me)
Jakob Hansen
Jakob Hansen
13,746 Points

Also, when you're trying to instantiate the class, lower and upper case, matters :) me = Student.name
It's probably just a typo :)

Ashley Keeling
Ashley Keeling
11,476 Points

the code is working in python but not in the challenge , it is saying :

Bummer! me doesn't seem to be an instance of Student

Jakob Hansen
Jakob Hansen
13,746 Points

You haven't capitalized the S

me = student.name

you are trying to acces the name attribute inside of the Student class. your class is called Student (with capital S) not student

Ashley Keeling
Ashley Keeling
11,476 Points

in the challenge I used a capital s but when I wrote it out on the comment , I missed the capital letter , and it is still saying

me doesn't seem to be an instance of Student