Introduction to Python Private Method
In Python, there three types of Python Private Method used in class:
Private members of a class are not accessible outside the class. Protected members are accessible within the class, as well as to its sub-classes. However, Public members can very well be accessed outside the class.
Points to be noted
- To define the private method, “__”(double underscore) is used in front of variables or function. So, they are not accessible to the outer environment of the class.
Example of private method:
class Detail: def __init__(self,FN,LN): self.__firstname = FN self.__lastname = LN print("Done!") d1=Detail("Rea","Messi") print(d1._firstname) print(d1._lastname)
- To define the protected method, “_”(single underscore) is used in front of variables or function. So, they are not accessible to the outer environment of the class. Example of protected method:
class Detail: def __init__(self,FN,LN): self._firstname = FN self._lastname = LN print("Done!") d1=Detail("Rea","Messi") print(d1._firstname) print(d1._lastname)
- This can be accessed directly from anywhere.
Example of public method:
class Detail: def __init__(self,FN,LN): self.firstname = FN self.lastname = LN print("Done!") d1=Detail("Leo","Messi") print(d1.firstname) print(d1.lastname)
All members are considered public by default in Python.
*Let’s see private variable and methods through some examples:
class DataScience: def __init__(self): self.name = 'Leo' self.__lastname = 'Henein def PrintName(self): return self.name +' ' + self.__lastname #Outside class DS = DataScience() print(DS.name) print(DS.PrintName()) print(DS.__lastname)
Here, __init__ is a reserved method which is known as a constructor. When the object of a class is instantiated, it runs automatically.
As one can notice, when we tried accessing the public variable outside the class, we were able to. However, when we attempted to access the private variable, we failed.
One more good point to notice here is, “ print(DS.PrintName()) “ succeeded. We are calling public function outside the class, but the public function is part of the same class to which private function belongs. Hence, the private variable can be accessed within the class by any of the functions belonging to the same class.
Let’s see one more example, without constructor “__init__” this time:
class DataScience: def PrintMe(self): return 'Printed' def __PrintMeNot(self): return 'Not Printed' #Outside class DS = DataScience() print(DS.PrintMe()) print(DS.__PrintMeNot())
If noticed, we are successfully able to access the “PrintMe” function, but not the “__PrintMeNot” function. That’s because “__PrintMeNot” is a private member function of class “DataScience”.
But there is still a way to access private members of class outside class. That is by “print(DS._DataScience__PrintMeNot() )” rather than “print(DS.__PrintMeNot())”.
This code below:
class DataScience: def PrintMe(self): return 'Printed' def __PrintMeNot(self): return 'Not Printed' #Outside class DS = DataScience() print(DS.PrintMe()) print(DS._DataScience__PrintMeNot() )
So to access the private members outside the class, the syntax should be:
So, we can say protection is still not much to private variables and methods.
- Private methods are used to help users serve with “Encapsulation”. That means methods accessible within the class and not outside of it.
- Encapsulation is the concept of OOP(Object-oriented programming). It basically wraps up the data and method into a single unit. This help in putting restriction over data and methods, so that accidental modifications can be avoided.
- In real life, when data is crucial and important, encapsulation becomes evident to use.
Rules and regulations for private method
Here are some rules related to the private method:
- As per rules, private variables are not accessible outside the class. However, as per the convention, a method or name prefixed with one underscore is treated as non-public. Hence, private members can be accessed outside the class by making it nonpublic like _classname__privatevariable. This is called “name mangling”.
- In a nutshell, we can say Python not truly supports the fundamentals of private.
- To summarize, public and private variables as well as methods:
|Public variable||Accessible from anywhere|
|Public method||Accessible from anywhere|
|Private variable||It is represented with two underscores at the start. It is accessible within the class.|
|Private method||This is also represented by two underscores at the start. It is accessible within the class.|
As we saw above, how private variables and methods play an important role in Python. It supports encapsulation and hence gives you more control to deal with data in real-life problems. It makes data accessibility flexible by making some data protected under modifiers like private, protected, and in public mode. Accidental access and modifications can be tackled well by using the concept of public, protected, and private. However, to use this accessibility well, one needs to be aware of its rules and regulations and go through PEP8 documentations, which may sometimes lead to fatal errors or memory leakage.
This is a guide to Python Private Method. Here we discuss the Advantages along with the Rules and regulations for Python private method. You may also have a look at the following articles to learn more –