In this tutorial we will discuss about encapsulation an important part of object oriented programming
is d encapsulation feature encapsulation is used to hide the values of these gate all or data object
inside a class preventing unauthorized access to them.
So encapsulation is about ensuring the safe storage of data data should be safe from changes by external
processes the encapsulation feature involves grouping attributes
and method into packages that contain different forms of access.
For example certain methods an attribute are packaged as public and so these attributes and methods
can be accessed by any class next Some attribute and method.
Packaged as protected that means only the child classes can access these attributes and methods which
are defined in the bad and class nixed certain attributes and methods are packaged as private which
means the methods and attributes can only be accessed by the class itself.
Now coming to the encapsulation feature in Python encapsulation and Python lacks strict access control
such as private and protected attributes Python interpreter will stop us from accidentally accessing
the attribute and the method.
But we can intentionally do anything as long as we are aware how the language works so in Python almost
everything about classes and objects is open open or public so we can easily inspect object Internet
and the objects are all accessible without any restrictions everything that is defined in the base class
is inherited and accessible from derived class and this exposes in done the implementation of an object
let us now see an example.
So we have the Department class which is the parent class of the superclass and we have another class
employee which is the derived class of the class department.
Now let us create an instance of the department class let's say the department is accounting.
This is an instance of the department class.
So let us access the attribute defined in this class using the instance accounting dot I.D. So here
is the value for this attribute.
Now we can change the value of this attribute using these instance.
So let's give another while due to this attribute and then display the attribute using the instance.
So we weren't able to change the value of an attribute using the instant so without interacting with
any or the method in a class we would able to change the value of an attribute.
And this is called breaking encapsulation and it is a bad practice for a programmer.
Because the class are the odd maintainer can no longer validate the data that is being accessed.
And this can cause unforeseen problems with a program that is dependent on the class it is inheriting
from so object where tables are attributes should not always be directly accessible to prevent accidental
change.
The attribute can sometimes only be change using the method the methods can ensure the correct values
are set.
And if an incorrect value is set the method can return and error python and relies on programming conventions
to indicate the intended use of an object.
So instead of having to enforce language rules it is left to the programmer to observe conventions and
these conventions are based on naming.
So let's understand this Python doesn't have the concept of private method there are no arbitrary restrictions
when accessing part of a class so there are no ways to impose a metered out at tribute to be strictly
private.
But to be able to have the encapsulation feature Python has a concept of hidden variables in which Python
does not restrict access but hinders access to certain variables by mutilating them.
Let us now see if you example to understand how Python uses naming convention to hinder access to attribute
or method.
So here we have defined a class test 1 and there is a variable defined in the init method of this class.
So if you observe there is a leading underscored a single leading underscored before the variable name
and when you see a variable.
Having a single leading underscore that means we should stay away from accessing these attribute or
methods but that does not mean that we cannot access these attributes so let us try accessing this Redburn
underscored a using an instance of this class.
Text one.
So we have created an instance of the test of the class test 1 and access this attribute containing
a single leading underscore and we were able to read the content of this variable and we can also change
the value to which this readable are the attributes pointing to two letters.
Go ahead and change the value of this variable and then display in Sweden.
So even though this variable has been marked as private using a single leading underscore we were able
to change the value of this variable externally that is from outside the class.
Let's see another example.
And here's another class based too.
In this class we have the init method.
We have defined two variables or attribute in the init method which have been marked as private using
naming convention one attribute has a leading single underscored and the added attribute has a leading
double underscore.
No let us create an instance of this class and try accessing both of these attributes.
So here we have created an instance of the class.
Now let us try accessing the private attributes defined in this class.
So let us first access the variable B
so we were able to see the content of this variable by directly accessing using an instance.
Now let us try to access the second variable double underscore D.
Now we have an exception drawn by the interpreter and the error is an attribute error and it says test
to object has no attribute double underscore D.
So in order to understand this error let us call the building function.
D I add in order to see the list of attributes for the instance test case 2.
So here we have the attributes for the instance test case 2.
This is the first attribute underscored b a single leading underscore before the attribute name and
then we also have the second attribute teal.
So if you'll notice the double underscore D attribute has been mutilated or converted by adding the
class name before the attribute any and the interpreter does this secretly and this concept of mutilating
the attribute name is known as name mangling.