+1

Python classes purpose

Hello Sololearners! Today is my first code and post. I'm using python 3 and didn't really understand the purpose of classes when following the course on classes. Therefore I trained myself with my first code and wanted some feedback. Is this a proper way to use classes? If I understood properly the purpose of classes is to allow multiple parameters to be assigned to an object and allow functions to modify these parameters independently? Is this correct? https://code.sololearn.com/coTjXKPdA2QD/?ref=app

8/9/2019 9:20:26 PM

Olivier Makart

4 Answers

New Answer

+2

I think the most important purpose is to protect a set of data and define how exactly it can be accessed and manipulated. Like this, you create units of data and related code that are - in a large program - easier to understand than if you had a thousand functions and a few hundred data items and would always have to remember what belongs to what and where you find it. Also you don't risk to destroy data by misuse - because you define the class in a way that this shouldn't be possible. Let me show an example of my own: https://code.sololearn.com/cW3SL4Esx5F8/?ref=app

+2

If you look at the bottom of the code, you see how the MusicBox can be used: You have a few 'buttons' - add, delete, next, song-list - and it's a piece of cake to use it. You don't even need to know how exactly the MusicBox works, because it does it all secretly. And that's sort of the leading idea with writing a class: You let it do the messy stuff and hide it all away until you have an easy-to-use gadget with just a few buttons.

+1

Hi. Thanks for the explanation about classes. It makes sense to me! About the code you posted, I didn't understand it fully. It's probably too early in my understanding of python. But at least I get what you mean. Thanks again

+1

Yes I did get that part about add, delete and shuffle :)