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

How do I access and print members in my instance?

I think I created my instance correctly.. not sure how to print out the name value though.

first_class.py
class Student:
    name = 'John'

Student() = me

3 Answers

Steven Parker
Steven Parker
231,269 Points

To create an instance, the new instance name goes on the left of the assignment, and the class name followed by parentheses goes on the right.

Then to access a property, you can use the membership operator (.) between the instance and property names.

You have a little mistake in what you are doing. You defined your class fine, but you are not creating the instance correctly.

It is true that calling Student() will create an instance of your class. But how do you assign that to me?

Think about it. If you wanted to set me equal to five, you would do: me = 5

So, if you want to set me equal to whatever was returned by Student(), how would you do that?

Once me is set correctly, where will the value of name be? It will be inside me, so me.name

Let us know if that isn't enough.

Thank you both I was able to spot my mistake!

Steven Parker
Steven Parker
231,269 Points

Glad I could help, and happy holidays!   :christmas_tree: