Name mangling is used to wipe the risk of conflict when designing classes for inheritance.
So if we had building classes for inheritance the subclasses using the same name for a private attribute
should not collide with the basic classes.
Names are attributes.
Next the Python interpreter applies.
Name mangling in order to protect the variable from being overridden in subclasses.
So all names that does attribute are methods that are defined in a class with double underscored is
automatically mongering to form a new name of the farm underscored.
Class
doubled it underscored attribute name and same is the case with a function which has two leading underscores.
So the interpreter.
Mangles the name of this function to underscore class the name of the class double underscored function.
So this provides a way for a class to define private variables and method.
Now let's get back to a good example.
So we have understood that in Python there is no strict mechanism for preventing access to private attributes
and methods.
So if you know the class name and the private attribute name you can access it using name mangling.
So how can this be done.
So we have just seen that we cannot directly access attributes that have leading double underscored.
You get this exception.
But you can access these variables de private variables using name mangling.
How can you do that.
This bad name has been name mango.
That means it is converted to the spawn class name underscore class name is Death to double underscored
D the variable double underscore D has been mangled to this form.
By the Python interpreter.
Now let us try accessing this variable using the spot.
Execute this.
Who.
Here we have the value of this variable which has been defined as private.
Using these double underscored convention.
So even though the variable is defined as a private variable.
If you know the class name and the attribute name we can access it directly it is generally not advised
to directly change the value of an attribute as it would violate the integrity of the object and the
preferred technique is to use methods exposed by the object to change the object properties.
So for that here we have defined class test three and in the inert method we have defined a variable
or an attribute e and we have assigned a value to.
So in order to access or read the content of this variable we have used the method get underscored 82
yard in order to change the value of this variable.
We are going to use the method set underscored 888 here.
We have created an instance of the class test 3.
Now in order to see the content of the variable e you can use the get attribute method.
So let's call this method using the instance.
So here we have the value of this variable E.
Now in order to change the value of this variable you can use the set attribute method.
So let's call this method set underscored attribute and we're going to pass 10 as the new value to this
method.
This is the new value of the variable e even though we have defined a method in this class in order
to change the value of the variables or the attribute defined in this class.
This does not stop us from directly accessing this variable.
Let us try accessing this variable using the instance of this class when you tried to access this variable
so we were able to read the content.
We can also change the value of this variable.
Let's see the new value is twenty three.
Now if you try to call the get attribute method using the instance as you can see the value has been
changed to the new value that is set without using the method defined in the class and which is a bad
practice.
And here's another example.
We have the class test for which has defined a private attribute using deleting double underscores.
We have also defined two methods in this class.
They get attribute method is used to retrieve the value of the variable the set attribute method is
used to change the value of the variable.
Now let us create an instance or the class Test forward.
So this is the instance test case forward.
And now if we access the private attribute underscored.
If using this instance the exception attribute error we get the attribute error because Python secretly
mangles the name all variables having leading double underscores.
Let us call the built in function D IDE and parse the instance name test case for.
So as you can see this is new name for the attribute double underscored.
Now if you want to retrieve the value in this variable you can use these methods defined in this class
get attribute method and the set attribute method in order to change the value of the bid.
Yeah we have another exam we have the best and class and independent Class B have defined a function
which has leading double underscores and then we have also defined a child class which inherits from
the parent class.
We have also defined a function or a method here.
The leading double underscores the method names in the child class and the parent class have the same
name.
Now let us create an instance of D child class.
Let's say this is Child 1.
Now using this instance we will call the double underscored function method.
So Child 1 is the instance underscored underscore if u n c.
Function and this function takes a value so this has drawn and enter and attribute error seeing the
child object has no attribute double underscore functions.
So letters call the desired function bus the child one argument and in the return list.
B c to increase your the underscored child double underscored function as the new name for the private
method defined in the child class next.
The second entry underscored parent double underscore function as the new name given to the method function
defined in the parent class so this function the function used has been inherited by the child class
because Python has secretly changed the name of the private method w underscored function in order to
access this function using the instance you need to use the new name.
So in order to call the method defined in the child class you give single underscored last name is child.
Double it underscored function and I'm going to pass the new value 10 and now the private method has
been called using name mangling using the new name.
So we were able to call the private method using name mangling.
One important point to note is name mangling is not applied.
If an attribute or a method name start and end with double underscore such as the double underscore
init method methods that have both leading and trailing double underscored are reserved for special
use in the python language as we know these methods are called the magic method.
And with this we come to the end of the topic encapsulation.