Encapsulation = wrapping the data

The OOPs the usage of encapsulation is very normal and easy. It is important to access the value of the variable through the function.

In the following simple example, we can get the value of name using get_name() function.

Some real time example like the following can explain the real importance of the encapsulation.


To read the JSON file and save it to the variable

import jsonclass JSONReader:
def __init__(self, file_path: str):
self.file_path = file_path
def read(self):
with open(self.book_path, 'r') as myfile:
data = myfile.read()
return json.loads(data)
if __name__ == "__main__":
reader = JSONReader("/tmp/a.json")

The file context manager can read the file as string, then json.loads(<string>) will convert the string value to the JSON object



» 6+ years of experience in Data engineering, Dashboard designing » 3+ years of experience in Web application development