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

Cant solve..

I dont understand this instance thing at all. I'd love to see the correct solution and an alternate explanation if possible?

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

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great. In fact, the only thing causing this to not pass is your indentation. Currently, you're trying to create an instance of the class within the class. Your me = Student() should be all the way to the left.

But let's talk a bit about instances. You will get to this more later, I promise. Right now, you are creating an instance of student and that Student instance will always have the name of "Lucas". It would be a fairly odd school to only accept students named Lucas. Later on (fairly soon I would imagine), you will learn how to give a student a name when you create them so that not all students are named "Lucas".

Students will share many common traits with each other. They will likely have a list of classes they are taking, a grade point average, a gender, social security number etc. But clearly, one student will not have the same social security number as another student. They might share the same name and they might even be identical twins, but they are unique people.

Take a look at this in a workspace:

class Student:
    name = "Lucas"

me = Student()
print(me.name)

#make another instance of student
other_student = Student()
#oh no! That student's name is "Lucas" by default
print(other_student.name)

#set the other student's name to "Jennifer"
other_student.name = "Jennifer"

#prints "Lucas"
print(me.name)
#prints "Jennifer"
print(other_student.name)

Here we have two unique instances of "Student". They are two separate individuals and have different names, but both are Students.

Hope this helps! :sparkles:

Ty so much for taking the time to explain that, very helpful!