Encapsulation In Python

aimtocode

Encapsulation Definition

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

In other way, it is a mechanism that associates the code and the data it manipulates into a single unit and keeps them safe from external interference and misuse.

In Python data and methods which access or manipulate the data are stored as single unit. and the basis of encapsulation is the class.

In class defines the structure and behavior (data and code) that will be shared by a set of objects.

Each object of a given class contains the structure and behavior defined by the class, as if it were stamped out by a mold in the shape of the class. For this reason, objects are somethimes referred to as instances of a class.

When you create a class, you will specify the code and data that constitute that class.

Python does not have the private keyword, unlike some other object oriented languages, but encapsulation can be done.

Example:



Output:

 Nokia Selling Price: 1000
 Nokia Selling Price: 1000
 Nokia Selling Price: 1100

In the above example, we have created an instance of Movile class and trying to modify the instance variable’s value, but it still gives the same value which set inside the constructor.

Example 2:



Output:

 Prayag
 India
 Player Prayag says hi from India
 Destroyed : Player  with id : 46739312


aimtocode