Tutorial 11 – Encapsulation in Python – Part 2
Welcome to the eleventh tutorial of Selenium-Python series! We will continue our study about encapsulation.What you will Learn:
- Private function
- Abstract base class (ABC)
Create ‘Engine’ class & create a private (using double underscore) class variable
Create a function ‘start’ as seen below
Create another class ‘Car’. Now a car has wheels & an engine, so we create a class variable ‘wheels’ and an object of ‘Engine’, see below
We now create a ‘private function’ __start (see lines 11-12 below)
We now set the capactity of the carr, lines 14-16 below
If we now try to call the private function __start() we get an error
“AttributeError: 'Car' object has no attribute '__start'”, see lines 18-19. So the private function won’t be accessed outside the class.
To access the private function we have to create another function ‘ignition’ (lines 12-13 below) and then call this function (line 21). Comment out line 20 & run program, we see the correct o/p in console
So we see that the private variables & private functions can be accessed inside the class (line 13 above) but not outside the class
Abstract base class (ABC)
From abstract base class module ‘abc’, let us import abstract base class ‘ABC’ or you may also call it as abstract superclass
Let us create ‘Hospital’ class that inherits ABC, see below
Now we will create 3 abstract methods and one non-abstract method in this class & see if it is possible to make abstract method & non-abstract method in the same class.
To make the abstract method, we have to use @ followed by the decorator ‘abstractmethod’, see line number 4 below. This automatically imports ‘abstractmethod’, see line 1
So create 3 abstract methods: operate, billing & scanning, see below. We will not define the implementation as of now, that is why we have written ‘pass’ in lines 6, 10 & 14. So abstract methods are basically incomplete. So we will not implement these functions in this class.
Let us create a non-abstract method now, see lines 16-17 below
We now create another class ‘RuralHospital’ & inherit ‘Hospital’ class, see below. We just write ‘pass’ in this class
We now create object of RuralHospital since it inherits all the properties of Hospital, see below.
When we try to call an abstract method (line 23 above), we get an error:
TypeError: Can't instantiate abstract class RuralHospital with abstract methods billing, operate, scanning
To fix this error, we have to implement the abstract method inside the ‘RuralHospital’ class, see lines 19-20 below
When we run now, we still get an error however ‘billing’ is no more mentioned in the error now, see below
Similarly we will implement remaining 2 abstract methods, see below. When we run, we no more get an error, see below
Similarly let’s create another class, see below (lines 24-32)
When we run, we see the below o/p
So we have to implement the abstract methods in sub-classes. Now comment lines 21, 22, 31, and 32 as seen below. Create object of ‘Hospital’ class & call any abstract method (lines 33, 34). We get the same kind of error
We can however call the non-abstract method from a sub-class, see lines 33, 34 below
In this tutorial we have studied about private functions & abstract classes. Thank you for reading!