My book that I'm self teaching from doesn't do the best job from the start of explaining what exactly classes are for and in what situations I would need to create my own class. So far the book has taught me the syntax by creating a Point class and a Television class. However, I'm just not grasping the why's and when's of creating my own class. I understand making a class that isn't supported in python by default...but what kinds of classes like that are necessary? can someone give some more examples and explain when/why you would need to implement them?
I know it's a little vague, if you need more information of what I'm trying to convey here please let me know. Thank you!
Understanding 'Classes'
The Point example should give you some clear hints on other classes you might want to write in the future. For example if you want to work with matrices you will want to make a Matrix class. You will realize that it is far easier, more intuitive and more error-proof to work with objects of type "Matrix" than working directly with 2-dimensional arrays. A "Player" class would be another simple example. It is preferable to have your data grouped in an object and manipulate it as a whole than having a sea of variables like a string for the name, an int for the health, an array for the inventory etc... Generally, you will want to create a new class every time you decide that you need a new, custom data-type. For more complex stuff classes are even more desirable. Did this help?
A little bit, I think. I was also doing some reading from various other sources. Basically I want a class whenever I want to wrap something that python doesn't already have built in that would otherwise be very complex in a nice little package with clear instructions for working with and editing. Does that sound reasonably close? I think I still need more practice with them for sure since this is my first time being introduced to them, but I prefer to understand why I need something before 'diving in'
Yes, you are close and it will all become clear when you write some programs on your own. Another advantage is that the data can (and in most cases should be) hidden (using 'private' or 'protected') so you can change the internals of the class without affecting the code that is using the class. A beginner book should explain all this, btw. Maybe it is not a book for beginners and assumes familiarity with the concepts of Object Oriented Programming...
Another advantage is that the data can (and in most cases should be) hidden (using 'private' or 'protected') so you can change the internals of the class without affecting the code that is using the class. A beginner book should explain all this, btw. Maybe it is not a book for beginners and assumes familiarity with the concepts of Object Oriented Programming...
It's a python book, and python doesn't have any concepts along the lines of private/protected members. Everything is public, everything is mutable, and you enforce data hiding by convention.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement