When do I actually use classes

Viewed 666

I've been coding for a few months now making simple scripts and a few bigger projects but I have never been able to wrap my head around when to actually use classes. It may be that my programs are to small to benefit from them but I just don't understand. Whenever I look up videos about it there is always the same kind of examples. When you have for example a student and you want to store information about it. But I don't understand how this could be implemented in a program. Can someone give an example on when it's useful to use a class and why it is better than not.

Example of student class:

class Student():
    def __init__(self, name, grade, gender):
        self.name = name
        self.grade = grade
        self.gender = gender

John = Student("John", "A", "Man")
2 Answers

Yes. I am very mush interested in classes and objects. And I can tell you use of classes and objects would really help you so much and reduce the lines of codes.

First of all, you should know where to use objects. Suppose, we have to create a game, then definitely we will have some objects in it (these are images that we perceive), but in addition to it there are more properties of an object, like on canvas, its x and y coordinates which change over time, when the object moves. And also, image of that object is itself a property of it.

For example, in Super Mario, you would see Mario as the main character and it is an object in terms of programming. Mario is an object with various properties and methods. What properties and methods? On the canvas (for drawing the image), it has x & y coordinates as I said earlier and in addition to this it has an image, width and height of the image, the methods (functions) like move right, move left, jump etc.

In short, you can say an object as a collection of properties (variables) and methods(functions).

Objects really help in organising all the codes. And more than that, classes help. Classes are templates for creating objects. See, in Super Mario, there are many enemies (I don't know what comes the first time brown one) and turtles and many others. You can notice that all of them have the same image(appearance), properties (size and shape), methods(movements) etc. All of them behave the same way.

So how to create many enemies of one type... Do we copy and paste the codes of object and change their properties (their coordinates are different in the game)? There we use classes... We define the properties and methods of objects that we want, by using variables and then pass different values to the class and create hundreds of objects of the same type.

Classes don't appeal when you are coding small programs. But say you are working on a game using the Pygame module, Classes would come in handy. For example, creating an in-game character using Class definitely makes life easier, as it packs all the information and properties into it. On the contrary, making changes to a game programmed using procedure-oriented code would be extremely hard and annoying. Not to mention the length of the code, you also have to clearly state every possible outcome and event. In short, Class is a beneficial means exclusively available to Object-Oriented-Programming, which is why it is a high-level programming language. Class may not be necessary in many cases, but is still an excellent tool to make your code shorter and easier to read and amend.

Related