Recently on our Data Analytics Ireland YouTube channel, we have been working hard to enhance our video content and delivery. As part of that process, we also looked at ways to understand classes and use them more efficiently.
In how to create a class in python we provided a video tutorial of the steps involved in Python on how to implement a class within your project.
Here in this blog posting, we will go through the different aspects of classes, and provide a practical example of Object-Oriented Programming, and how it can help you to manage and reduce the code you may have to write.
Before we start, the first question we should ask is, what is a class?
According to the Official Python Website “Classes provide a means of bundling data and functionality together”. So in essence, what they are really saying is that they are used to centralise information and functionality around a python object.
So how would we describe an object? An object is anything that can have attributes attached to it and have some functionality that allows the object to function. Most objects will have methods associated with them, and these are the functionality of the object. So let’s step back a second and show this in a piece of code:
“Car ” above is the object, and it has attributes of type, color, wheels and doors. Other attributes can be added at any point. So you may add why is start structuring like this important?
The pure and simple answer is organisation!
The reason behind this way is that as the car and its details are all in one place it encourages:
- Consistency – everything about a car is documented in one place.
- No duplication – If you were referencing the object car in a number of places in your code, for each update, you would have to change it in each place, it makes the updates long and harder as you have to remember where you put it in the different parts of your computer program.
- Can be called from anywhere – As we have one version of the object car, now anywhere in our code we can call it and use its attributes, as there is only one version makes the program a lot easier to manage.
Now that we have looked at attributes, what about the methods and functions that can be contained within them?
The methods and functions will operate like any other method or function, but the difference when they come to objects is:
They are specific to that object!
In the above code, you will see that there are three methods, and all are specific to the object car.
For example, you would not expect to see any methods that would relate to pumping up the wheels or changing a bulb, purely because this object car is only concerned with the functionality of the car.
To build on my point above around no duplication, if this object was not created, this last piece of code might have to be maintained and duplicated a number of times within your code, this is where the classes come into their own
So say I want to use the class and its attributes, how would I go about doing that?
In section 9.3.5. Class and Instance Variables on Python classes, it states the following:
Instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:
Anywhere in your code, all you need to do is create a variable and equal it to the class, see below. Lines 22,23,24, all can now use the methods that are in the class, and their respective functionality. As can be seen, lines 25,26,27,28 all bring in the attributes of the class to be used, by putting them in under the “def__init__” method.
And here is the output of the above. After initiating the class, we have assigned its attributes into a new variable and printed.
And there you go we have initialised the class Car and used its attributes and methods outside the class in our regular programming, and this could be done anywhere in our program, multiple times, but only having to use one class.
You can see this working below: