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

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

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

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

All 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
Son-Hai Nguyen
2,481 Points

Thank you Frederik and Chris. So it sounds like the biggest advantage of classes is inheritance right?