In the last tutorial, we’ve learned about Methods in Python. To further increase the re-usability of your code, it is also possible to use a Class in Python. With Classes, you can encapsulate your methods into better re-usability.
The Class in Python
With a class, we can add several methods together. Imagine you write an app for selling different kind of vehicles – either a car, a bike or a motorcycle. There are several items in your vehicle, that are always the same – e.g. all would have a wheel. With a class, you can sum this up and create re-usability on each of them. Basically, a class in Python is also very similar to classes in C-like languages. However, there are several differences. The most striking one is that we don’t have any public-protected-private modifiers. This applies to the class itself and also to its methods and variables. Basically, a class is defined with the “class” keyword. The syntax of it looks like this:
As you can see, we have several possibilities with classes. First of all, defining a class is easy. You would then specify some variables within the class. The constructor – one thing you often use in C-like languages – is written wit “def__init__(“. You will then have the possibility to specify variables in the brackets. One mandatory variable in there is the “self”. This refers to the class itself and makes all objects in the class accessible. You will also need it in all further method definitions.
Let’s go back to the previous sample with the vehicles. In order to achieve that, we create a class “Vehicle” and add some variables and methods to it. The Class should look like the following. Create a new Notebook and name this notebook “vehicle”.
class Vehicle: vname = "Not Defined" curspeed = 0 def name(self): print(self.vname) def accelerate(self, speed): self.curspeed += speed
Now comes the tricky part: since we are working in a notebook application and not in a comprehensive IDE, we first need to download the file as “Python” and then re-upload it. If you would just rename the notebook, it would result in a json document that can’t be read. We re-upload the file and name it “vehicle.py”. Make sure that it is in the same folder as your other notebook. We then switch back to your original file and use the just created vehicle:
from vehicle import Vehicle v = Vehicle() v.name()
Note that the output is as expected: we explicitly named the vehicle “Not Defined” – so it isn’t an error. I guess you can imagine what I want to do with the vehicle – I want to use inheritance in the next sample 🙂
Inheritance in Python
Often, it is necessary to inherit from a class. This is helpful, when a sub-class is a specialisation of another class and thus has some common functions or behaviour. It is therefore easier to extend the behaviour without re-writing the code. To enable inheritance, we add brackets to the new class with the name of the parent-class. Also, it is important to import the parent class. Everything else stays the same.
In the following sample, we create a new class and name it “car”. Basically, we extend the behaviour of our vehicle and add a “start” method to it. This will print a statement that the car has started with a specific number of HP. Create a new file in Jupyter and name it “car”:
from vehicle import Vehicle class Car(Vehicle): def __init__(self, brand, hp): self.hp = hp vname = brand def start(self): print("Engine of " + self.vname + " started @ " + str(self.hp))
Do the same steps as in the “vehicle” file and go back to your initial Notebook. We instantiate a new Car and give it a name and some HP:
from car import Car c = Car("BMW", 150) print(c.hp) print(c.name())
The function “hp” belongs to the “car”-class, whereas the function “name” belongs to the “vehicle” class. Let’s now use the “start” function:
Engine of Mercedes started @ 150
Also here we use the method of our “car” Class in Python.
Since now the code got more complex, it is necessary to think about how to work with Errors – in our next tutorial, we will look at error handling.
If you are not yet familiar with Spark, have a look at the Spark Tutorial i created here. Also, I will create more tutorials on Python and Machine Learning in the future, so make sure to check back often to the Big Data & Data Science tutorial overview. I hope you liked this tutorial. If you have any suggestions and what to improve, please feel free to get in touch with me! If you want to learn more about Python, I also recommend you the official page.