Python Tutorial for Beginners 26 - Python Encapsulation


 hey guys welcome to the next blog on Python to tutorial for beginners in this blog we will learn how to use encapsulation in Python and capsulation is one of the most important principle in object-oriented programming so let's get started I will be using this car class and the rectangle class which we have been using in the previous blogs also and let me remove these print statements because we don't want them right now now let's say we want to change the value of speed so if you want to change the value of speed you can simply take your object for example for dot your attribute name for example speed and then you can directly change it for example 300 and when I run the program it's going to print the new speed which is 300 initially we have set the speed to 200 in here we can also put let's say the speed as string which is very stupid but will it work it works you can see here and this can break the function in which the speed is used so therefore it is very important that we protect our data and only give the access to our data to the other users so encapsulation is particularly important when you want to give your code to some other people because they might want to change your code so let's see how we can encapsulate our code so to encapsulate our code we create functions so let's create our first function which is to set the value of speed so I'm going to just say set underscore speed and inside the parentheses it's going to take self as the first argument which is the default and second is the value of speed so so I can say value here right and then once we have the value of speed we can just call self dot speed is equal to value similarly we can create our get method for the speed so right now we have created the set method to set the speed and now we are going to create a get method to get the speed and in order to get the speed we don't need to pass any argument we just need to return this speed which is already said so we can just write return and then self dot speed so we have created two functions set and get these are also called setter and getter for the attribute which is speed in our case so let's use these functions instead of this kind of setting of data so first of all I am going to call this for object dot set speed method out of this object and then I am going to pass the new speed using this set method now in order to get the speed I can write instead of this for dot speed I can just write the for dot get speed which is going to give me the speed of this Ford object so I'm going to run the code and you can see it prints the speed which we have set using the set method but still will this protect your data let's see so I'm going to once again try to use for dot speed and then I will try to set the new value of speed will it be a problem No so you can see when I try to get the speed the new speed is set and we want to prevent this kind of setting of data using this equals to symbol so we need to somehow make our attribute private now if you are familiar with other object-oriented programming languages like Java or C++ they use the keywords like public private or protected in order to mark their attributes or the member variables as the private or public or protected but Python doesn't have any of these keywords so how we can make our data private so let me give you first of all an example so I am going to go to this class which is hello and Here I am going to set some values so first value is for example self dot a is equal to 10 second value is self dot underscore B is equal to 20 and the third value is self dot underscore underscore C is equal to 30 so we have created three member variables in this hello class one is a second is underscore B and the C is with two underscores in front of it and let's try to access all these three values so I am going to just use the print method and then the hello object dot a and similarly I will use this print to get the value of dot underscore B and also dot underscore underscore C now let's try to run this program and let's see what happens and you can see here the first value which is printed here is 10 which is the value inside the a variable so this code was executed successfully now we can see here that 20 is also printed that means this code is also executed successfully but at the time of underscore underscore C when we want to use this underscore underscore C outside the class it says hello object has no attribute called underscore underscore C and this is the point so underscore underscore you can use to make your attribute private okay so this is our convention there is no keywords like private in Python that's why python uses this kind of convention in which whenever you use double underscores it makes your data private when you use single underscore that also means that it's a private variable but this is only a convention nothing stops you to change the value of underscore B or accessing the value of underscore B okay so if you truly want to make your data private then use double underscore in front of your variable when you use single underscore that means it's a partially private variable and it's only a convention okay so let's go to our car class once again and now we know that we can add double underscore in front of our variable name so we have two variables here one is speed so let us add double List underscore in front of speed and also let's add double underscore in front of this color variable to make both of them private so I'm going to change this value of speed in getter and setter also and now let's try to access this value using this Ford object which is underscore underscore speed now let's try to run our program so I'm going to change the file and then run our program and you will see that it's going to give us an error and this error says the car object has no attribute called color because this variable color is private now also you will observe that the value of the speed is 300 which is set by this set function even though we have set the speed again to 400 using this underscore underscore speed but this speed is not changed right so we cannot change the speed using this underscore underscore variable because now it's a private variable and that means we cannot change the value using this variable we need to use this set function in order to change the value of the speed also if you want to use this underscore underscore color here and let's run the code this will also give you an error because this underscore underscore color is private so either you can remove this code or you can create the setter and getter method for the color also so let's create the setter and getter method for the color and now we can access the value of color using the get color method right so instead of this get speed we can just write for dot get color and it's going to give us the value of color and now you can see that there is no error and the speed here is 300 which we have set using the set speed method and color is red which is the color we have given using this argument in the constructor and this type of restricting your data access using functions is called encapsulation let's do the same thing with our rectangle class also and let's create the setter and getter method inside the rectangle class so you already know we can use this define and then we can first of all set for example set the height first of all and then we can give the value of height here and then self dot height is equal to height right and don't forget you need to give double underscore in front of your attributes in order to make them private so I'm going to do just that and let's create the get height method also so now we have created the setter and getter for the height and let's do the same for the width also so now you have made the height and width as private and we have created setter and getter for the height and width and now in order to calculate the area we can define one more method here which is the area method so let's create the area method here and this area method is not going to take any argument we are just going to return the multiplication of the height so self dot height multiplied by self dot underscore underscore width okay which will give us the area of this rectangle and we can call this area method instead of using this kind of notation so direct one dot area and rect two dot area also let's run the code by changing the file name here and run this code and you can see the area is printed now so this is how you can use encapsulation in python to hide your data I hope you have enjoyed this blog I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News