1 00:00:00,120 --> 00:00:07,110 Name mangling is used to wipe the risk of conflict when designing classes for inheritance. 2 00:00:08,130 --> 00:00:16,800 So if we had building classes for inheritance the subclasses using the same name for a private attribute 3 00:00:17,040 --> 00:00:20,920 should not collide with the basic classes. 4 00:00:21,090 --> 00:00:23,360 Names are attributes. 5 00:00:23,790 --> 00:00:27,210 Next the Python interpreter applies. 6 00:00:27,210 --> 00:00:33,580 Name mangling in order to protect the variable from being overridden in subclasses. 7 00:00:33,780 --> 00:00:43,250 So all names that does attribute are methods that are defined in a class with double underscored is 8 00:00:43,340 --> 00:00:50,700 automatically mongering to form a new name of the farm underscored. 9 00:00:50,740 --> 00:00:51,490 Class 10 00:00:54,110 --> 00:01:04,360 doubled it underscored attribute name and same is the case with a function which has two leading underscores. 11 00:01:04,480 --> 00:01:07,300 So the interpreter. 12 00:01:09,150 --> 00:01:20,870 Mangles the name of this function to underscore class the name of the class double underscored function. 13 00:01:21,030 --> 00:01:28,830 So this provides a way for a class to define private variables and method. 14 00:01:28,830 --> 00:01:31,260 Now let's get back to a good example. 15 00:01:31,860 --> 00:01:39,000 So we have understood that in Python there is no strict mechanism for preventing access to private attributes 16 00:01:39,120 --> 00:01:40,410 and methods. 17 00:01:40,950 --> 00:01:50,200 So if you know the class name and the private attribute name you can access it using name mangling. 18 00:01:50,260 --> 00:01:52,230 So how can this be done. 19 00:01:52,270 --> 00:02:02,890 So we have just seen that we cannot directly access attributes that have leading double underscored. 20 00:02:02,940 --> 00:02:04,540 You get this exception. 21 00:02:05,410 --> 00:02:12,840 But you can access these variables de private variables using name mangling. 22 00:02:12,950 --> 00:02:14,300 How can you do that. 23 00:02:14,330 --> 00:02:20,570 This bad name has been name mango. 24 00:02:20,670 --> 00:02:32,040 That means it is converted to the spawn class name underscore class name is Death to double underscored 25 00:02:32,520 --> 00:02:40,220 D the variable double underscore D has been mangled to this form. 26 00:02:41,580 --> 00:02:43,800 By the Python interpreter. 27 00:02:43,800 --> 00:02:47,740 Now let us try accessing this variable using the spot. 28 00:02:49,360 --> 00:02:50,510 Execute this. 29 00:02:50,520 --> 00:02:50,850 Who. 30 00:02:50,860 --> 00:02:57,910 Here we have the value of this variable which has been defined as private. 31 00:02:57,940 --> 00:03:02,180 Using these double underscored convention. 32 00:03:02,350 --> 00:03:07,600 So even though the variable is defined as a private variable. 33 00:03:08,110 --> 00:03:17,150 If you know the class name and the attribute name we can access it directly it is generally not advised 34 00:03:17,160 --> 00:03:27,240 to directly change the value of an attribute as it would violate the integrity of the object and the 35 00:03:27,240 --> 00:03:36,360 preferred technique is to use methods exposed by the object to change the object properties. 36 00:03:36,360 --> 00:03:45,600 So for that here we have defined class test three and in the inert method we have defined a variable 37 00:03:45,780 --> 00:03:50,360 or an attribute e and we have assigned a value to. 38 00:03:50,940 --> 00:04:00,600 So in order to access or read the content of this variable we have used the method get underscored 82 39 00:04:00,600 --> 00:04:05,280 yard in order to change the value of this variable. 40 00:04:05,280 --> 00:04:10,580 We are going to use the method set underscored 888 here. 41 00:04:10,770 --> 00:04:16,560 We have created an instance of the class test 3. 42 00:04:16,680 --> 00:04:25,050 Now in order to see the content of the variable e you can use the get attribute method. 43 00:04:26,130 --> 00:04:29,400 So let's call this method using the instance. 44 00:04:29,400 --> 00:04:33,680 So here we have the value of this variable E. 45 00:04:33,750 --> 00:04:41,070 Now in order to change the value of this variable you can use the set attribute method. 46 00:04:43,220 --> 00:04:54,660 So let's call this method set underscored attribute and we're going to pass 10 as the new value to this 47 00:04:54,660 --> 00:04:55,540 method. 48 00:04:55,560 --> 00:05:02,670 This is the new value of the variable e even though we have defined a method in this class in order 49 00:05:02,670 --> 00:05:08,600 to change the value of the variables or the attribute defined in this class. 50 00:05:08,610 --> 00:05:13,520 This does not stop us from directly accessing this variable. 51 00:05:13,680 --> 00:05:22,230 Let us try accessing this variable using the instance of this class when you tried to access this variable 52 00:05:23,010 --> 00:05:25,920 so we were able to read the content. 53 00:05:25,920 --> 00:05:33,690 We can also change the value of this variable. 54 00:05:33,830 --> 00:05:37,630 Let's see the new value is twenty three. 55 00:05:37,660 --> 00:05:48,560 Now if you try to call the get attribute method using the instance as you can see the value has been 56 00:05:48,560 --> 00:05:56,330 changed to the new value that is set without using the method defined in the class and which is a bad 57 00:05:56,510 --> 00:05:59,590 practice. 58 00:05:59,660 --> 00:06:02,110 And here's another example. 59 00:06:02,210 --> 00:06:10,630 We have the class test for which has defined a private attribute using deleting double underscores. 60 00:06:10,670 --> 00:06:14,160 We have also defined two methods in this class. 61 00:06:14,240 --> 00:06:21,410 They get attribute method is used to retrieve the value of the variable the set attribute method is 62 00:06:21,410 --> 00:06:26,480 used to change the value of the variable. 63 00:06:26,480 --> 00:06:33,260 Now let us create an instance or the class Test forward. 64 00:06:34,220 --> 00:06:38,150 So this is the instance test case forward. 65 00:06:38,360 --> 00:06:44,920 And now if we access the private attribute underscored. 66 00:06:44,990 --> 00:06:56,360 If using this instance the exception attribute error we get the attribute error because Python secretly 67 00:06:57,620 --> 00:07:03,260 mangles the name all variables having leading double underscores. 68 00:07:03,470 --> 00:07:13,010 Let us call the built in function D IDE and parse the instance name test case for. 69 00:07:13,150 --> 00:07:20,140 So as you can see this is new name for the attribute double underscored. 70 00:07:20,560 --> 00:07:28,480 Now if you want to retrieve the value in this variable you can use these methods defined in this class 71 00:07:29,020 --> 00:07:39,060 get attribute method and the set attribute method in order to change the value of the bid. 72 00:07:39,440 --> 00:07:50,200 Yeah we have another exam we have the best and class and independent Class B have defined a function 73 00:07:50,800 --> 00:08:01,570 which has leading double underscores and then we have also defined a child class which inherits from 74 00:08:01,570 --> 00:08:03,210 the parent class. 75 00:08:03,250 --> 00:08:07,960 We have also defined a function or a method here. 76 00:08:08,030 --> 00:08:17,440 The leading double underscores the method names in the child class and the parent class have the same 77 00:08:17,590 --> 00:08:18,760 name. 78 00:08:18,790 --> 00:08:24,670 Now let us create an instance of D child class. 79 00:08:24,670 --> 00:08:26,560 Let's say this is Child 1. 80 00:08:27,370 --> 00:08:34,200 Now using this instance we will call the double underscored function method. 81 00:08:34,240 --> 00:08:39,940 So Child 1 is the instance underscored underscore if u n c. 82 00:08:39,950 --> 00:08:55,730 Function and this function takes a value so this has drawn and enter and attribute error seeing the 83 00:08:55,730 --> 00:09:01,160 child object has no attribute double underscore functions. 84 00:09:01,250 --> 00:09:12,800 So letters call the desired function bus the child one argument and in the return list. 85 00:09:12,850 --> 00:09:21,060 B c to increase your the underscored child double underscored function as the new name for the private 86 00:09:21,070 --> 00:09:26,930 method defined in the child class next. 87 00:09:27,020 --> 00:09:35,690 The second entry underscored parent double underscore function as the new name given to the method function 88 00:09:35,990 --> 00:09:47,070 defined in the parent class so this function the function used has been inherited by the child class 89 00:09:47,580 --> 00:09:56,970 because Python has secretly changed the name of the private method w underscored function in order to 90 00:09:57,030 --> 00:10:04,840 access this function using the instance you need to use the new name. 91 00:10:04,850 --> 00:10:17,070 So in order to call the method defined in the child class you give single underscored last name is child. 92 00:10:17,350 --> 00:10:29,150 Double it underscored function and I'm going to pass the new value 10 and now the private method has 93 00:10:29,150 --> 00:10:34,070 been called using name mangling using the new name. 94 00:10:34,160 --> 00:10:41,240 So we were able to call the private method using name mangling. 95 00:10:41,480 --> 00:10:47,030 One important point to note is name mangling is not applied. 96 00:10:47,030 --> 00:10:55,880 If an attribute or a method name start and end with double underscore such as the double underscore 97 00:10:56,000 --> 00:11:04,520 init method methods that have both leading and trailing double underscored are reserved for special 98 00:11:04,520 --> 00:11:11,630 use in the python language as we know these methods are called the magic method. 99 00:11:11,630 --> 00:11:17,660 And with this we come to the end of the topic encapsulation.