Give an example of encapsulation in Python
The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions, which are wrapped in the class, can access it. These functions provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding.
class Encapsulation: __name = None def __init__(self, name): self.__name = name def get_name(self): return self.__name pobj = Encapsulation('Rocky') print(pobj.get_name())
Sample output of above program.
Rocky
2019-07-01T01:00:19+05:30
2019-07-01T01:00:19+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution