Private Methods in Python

Private methods are used to help users serve with “Encapsulation.” This means that methods are accessible only inside the same class and not from outside it. In Python, private methods cannot be accessed/called outside the class.

This post provides an in-depth guide on private methods using numerous examples and following the below contents:

  • What are Private Methods in Python?
    • Differences Between Private and Public Methods
  • Creating Private Methods in Python
  • Accessing Private Methods in Python
  • Call Private Methods Outside the Class

What are Private Methods in Python?

In Python, private methods can only be called inside the same class where they are initialized. They cannot be called outside the class, even by subclasses. Private methods are denoted by double underscores (__), as shown below:

class class_name:
    def __private_method(self):
        # do something

Python’s private methods can be used for the following purposes:

  • A class’s internal workings are hidden from the outside world by using private methods.
  • They encapsulate the class’s behavior, preventing other classes from directly accessing or modifying the class’s state.

Differences Between Private and Public Methods

The difference between private and public methods are shown below:

Private MethodsPublic Methods
private methods can only be accessed/called inside the class.Public methods can be called inside the class.
private methods are used to implement its internal behavior.Public methods are used to define the external interface of a class.
private methods can only be called by other methods within the same class.Public methods can be called by any object.

Example 1: Creating Private Methods in Python

To create a private method, the member name must be prefixed with the double underscore “__.” Here is an example of how to create a private method in Python:

Code: 

class school:
    def __private_method(self):
        print("This is a private method")

    def public_method(self):
        print("This is a public method")
        self.__private_method()
obj = school()
obj.public_method()
  • The class defined two methods named as “ __private_method” and “public_method”.
  • The private method named “__private_method()” is defined using the double underscore at the start.
  • The private is accessed inside the public method using the expression “self.__private_method()”.
  • The class object named “obj” is used to call the public method.

Output:

The private and public methods are created and accessed using the class object.

Example 2: Accessing Private Methods in Python

Private methods cannot be accessed directly outside the class. However, they can be accessed indirectly using other methods within the class. For example, a public method within the same class can call a private method using the below code example:

Code:

class Students:

    def free(self):
        print("Public method")

    def __free(self):
        print("Private method")
   
    def boys(self):
        self.free()
        self.__free()
my_obj = Students()
my_obj.boys()
  • The class “Students” is created using the keyword class.
  • The public method, named “free,” and the private method, by prefixing with a double underscore, are defined.
  • The third method, named “boys,” is defined.
  • Inside the third method, the public and private method is accessed using the self parameter.
  • The class object is used to access the third method, automatically accessing the private and public methods.

Output:

The private method has been successfully accessed.

Example 3: Call Private Methods Outside the Class (Name mangling)

The below example is used to call private methods using the Name mangling techniques. The private method is called from outside the class with the help of this technique.

The private method is created using the double underscore, just as shown below:

__method

We need to replace the above with the given below syntax:

_classname__method

 In the above case, the “_classname__method” replaces “__method,” where the class name is the current class name.

Now. let’s understand it via the following examples:

Code:

class School:
    def method(self):
        print("This is a Public method")
    def __method(self):
        print("This is a Private method")
obj = School()
obj._School__method()
  • The class name “School” is created at the start.
  • The name mangling method, such as “obj._School__method(),” is used to access private methods outside the class.

Output:

The private method has been accessed successfully.

Conclusion

A private method in Python can only be accessed/called within the same class in which it is defined. Private methods are denoted by double underscores (__). Accessing private methods directly from outside of a class is impossible. Alternatively, they can be accessed indirectly through other class methods. The class’s private methods can also be accessed through other methods, such as name mangling. This tutorial provided an in-depth overview of Python’s private methods.