+ 2

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

9th Aug 2019, 9:20 PM
Olivier Makart
Olivier Makart - avatar
5 odpowiedzi
+ 3
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
9th Aug 2019, 10:08 PM
HonFu
HonFu - avatar
+ 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.
9th Aug 2019, 10:19 PM
HonFu
HonFu - avatar
+ 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
9th Aug 2019, 10:15 PM
Olivier Makart
Olivier Makart - avatar
+ 1
Yes I did get that part about add, delete and shuffle :)
9th Aug 2019, 10:21 PM
Olivier Makart
Olivier Makart - avatar
0
Omkar Chougale, please don't hijack the question thread of another user. You can open a question of your own. Be sure that you properly describe your problem and show your code. https://www.sololearn.com/discuss/1316935/?ref=app
1st May 2020, 11:29 AM
HonFu
HonFu - avatar