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)
  • Conclusion

Private function
Create ‘Engine’ class & create a private (using double underscore) class variable

Figure 1

Create a function ‘start’ as seen below


Figure 2

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


Figure 3

We now create a ‘private function’ __start (see lines 11-12 below)


Figure 4

We now set the capactity of the carr, lines 14-16 below


Figure 5

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.


Figure 6

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


Figure 7

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


Figure 8

Let us create ‘Hospital’ class that inherits ABC, see below


Figure 9

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


Figure 10

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.


Figure 11

Let us create a non-abstract method now, see lines 16-17 below


Figure 12

We now create another class ‘RuralHospital’ & inherit ‘Hospital’ class, see below. We just write ‘pass’ in this class


Figure 13

We now create object of RuralHospital since it inherits all the properties of Hospital, see below.


Figure 14

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
Figure 15

To fix this error, we have to implement the abstract method inside the ‘RuralHospital’ class, see lines 19-20 below


Figure 16

When we run now, we still get an error however ‘billing’ is no more mentioned in the error now, see below


Figure 17

Similarly we will implement remaining 2 abstract methods, see below. When we run, we no more get an error, see below


Figure 18

Similarly let’s create another class, see below (lines 24-32)


Figure 19

When we run, we see the below o/p


Figure 20

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


Figure 21

We can however call the non-abstract method from a sub-class, see lines 33, 34 below


Figure 22

Conclusion
In this tutorial we have studied about private functions & abstract classes. Thank you for reading!