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

Chamundeshwari Vadamalai
Chamundeshwari Vadamalai
2,204 Points

what is a class instance

what is class instance? and what is wrong with this code?

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

1 Answer

diogorferreira
diogorferreira
19,363 Points

The best way I can explain this to you is, imagine a class being the blueprint to create your house it holds the "structure" of the house (this being the functions you use) and the instances of that class being the houses you build using that blueprint. They all have the same structure (because they used the same blueprint) but are all unique because you can set attributes to different things. Here's a link that should help you understand it better: click here

Now in terms of your code:

class Student:
    name=str("Chamu")
me=Student.name()
print(me)
  • You wouldn't need to call str() on the name, it already is a string.
  • When making an instance of a variable all you need to do is assign me = Student() otherwise you'd be assigning me to the name attribute not the class
  • Finally you print the new instances' name through me.name
class Student:
    name = "Chamu"

me = Student()
print(me.name)