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!
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

Son-Hai Nguyen
2,481 PointsWhy should we use class?
I think I'm missing pretty much the whole point of this section. Can someone please tell me why should we use class? As far as I see, I can still live well with built-in classes. If the point of def
is avoiding repeating yourself, what can I use class
for?
Thank you guys!!!
2 Answers

Chris Freeman
Treehouse Moderator 68,389 PointsAll objects in Python contain persistent values or state. A str
contains characters, an int
or float
contain numeric values, a list
or set
contain a collection of references to other objects. These are all classes.
Using def
to define a function, creates a way to operate on objects, that is, to affect them or create other classes based on them.
A class
is defined if a new object is needed to store state in a way not possible by the built-in classes. The class methods allows for controlling the specific ways the state of a class instance may be modified. All of the state and methods of a class are considered to be attributes of the class.
A user
on a website is a common example of a class where the state of the user contains user information, privileges, activity history, etc. plus the methods to modifying the state. Additionally, these class attributes may references to other class instances, and so on.
Post back if you have more questions. Good luck!!

Son-Hai Nguyen
2,481 PointsThank you Frederik and Chris. So it sounds like the biggest advantage of classes is inheritance right?
Frederik Hulleman
PHP Development Techdegree Graduate 15,232 PointsFrederik Hulleman
PHP Development Techdegree Graduate 15,232 PointsHi, just to make sure I understand your question: are you struggling with the concept behind object-oriented programming? If yes, I really like this article: https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/
I hope it's useful to you as well! If not, let me know. Good luck!