Python Tutorial for Beginners 27 - Private methods in Python

 


hey guys welcome to the next blog on Python tutorial for beginners in the last blog we have seen how to use encapsulation in Python and we have also seen how we can declare private member variables in Python to declare a private member variable we use double underscore in front of your variable name and this variable becomes private but the question is what exactly is a private member variable so a private member variable is private to the class that means you can use this private member variable inside the class but as soon as you use it or access it outside the class it will give you an error so you cannot access a private member variable outside a class and the example we have already seen so here you can see I try to access this private member variable outside the class and when I run the code it says that there is no underscore underscore C attribute for this hollow object why because whenever you create an instance and if the member variable is private this private member variable is invisible to your object so you cannot access the private member variable outside the class but can be used this private member variable inside the class let's see so I'm going to declare one more method here and I'm going to name it as public underscore method and this doesn't take any argument but what we have learned about the methods in a class that even though you don't pass any argument to the method you have to provide at least one argument which is the self argument and what is self self is the current object so let me just print something here quickly for example public here so can use this private member variable inside this method let's see so I'm going to just write self dot underscore underscore C and then I'm going to run this code and let me comment this line because this was giving us an error and instead we can use this public method so I can use an instance of hello which is hello dot public method and let's run the code now and you can see there is no error that means you can use your private member variables inside the class or any method of the class now the other question is what is a public member variable like a here so a is a public member variable because it doesn't contain any underscore underscore in front of it and that means you can use this public member variable inside the class as well as outside the class so if I want to use this public member variable inside the class also I can just use self dot a and then run the code and it's totally fine it doesn't give us any error let's print the value of for those two member variables and once again run the code and it will print the value of a and C and then this message which we are printing here which is public here now the next question is how we can define a private method in the class so let's see how we can define a private method you already know most probably which is by using double underscore in front of your method name so for example underscore underscore private underscore method and then I'm going to just print something inside this method let's say private so this is a private method and how you can define a private method using these double underscore in front of the name of your private method and the restriction will be the on the private methods also so let's try to access this private method outside the class so I have this hello instance and when I press dot here you will see there is no suggestion for this private method because PI Chum already know that this is a private method so it doesn't show this method in the suggestion when you use this object outside the class but let's try the brute force and let's try to use this method outside the class and when I run this code you will see once again you will get an error which says hello object has no attribute underscore underscore private method so private methods also you cannot use outside the class but inside the class absolutely you can use your private methods so how to use your private methods inside that lass so you can use your private method inside the class using once again a self keyword so you write self dot and then your method name for example private method and that's how you can call your private method inside that last using self so any method you want to call inside a class you do it once again using the self keyword as you do with your member variable and once again when I run the code now it prints private using this statement and this statement is executed when we call this private method from here so I hope this blog will clarify some more doubts about the private member variables and private methods in Python and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News