
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private Methods in Python
In object-oriented programming, private methods are functions that act as access modifier within a class designed only for internal use and not accessible from outside the class. The functionality of private methods is primarily meant for encapsulation which means that they are hidden to prevent unwanted modifications and misuse.
Working of Private Methods
In Python, the convention to denote private methods is "_". We just need to prefix the method name with a single underscore (_) or, double underscores (__).
-
Single underscore(_) : To indicate that the method is meant for internal use. It still can be accessed from outside the class.
-
Double underscore(__) : Cannot be accessed outside the function with the function name. It still can be accessed through "Name Mangling" method to make it harder to access from outside the class.
Using a Private Method
To define a private method we have to prefix the member name with a single underscore ("_")or, a double underscore ("__"). If we use a single underscore we can only access it within the class. Whereas, using double underscore, allows us to access the method from any base class.
Example
In the below example code, __private method is a private method. we cannot call it directly from outside of the class MyClass, but we can call it within the class itself like in public_method.
class MyClass: def __init__(self, value): self.value = value # Private method def __private_method(self): return self.value ** 2 # Public method def public_method(self): return self.__private_method() obj = MyClass(5) print(obj.public_method())
Output
25
Single Underscore Private Method
If a private method is prefixed with a single underscore that indicates it was intended to be "private" by convention and won't encourage to call it directly from outside the class.
Example
In the below example code the public_method calls the _private_method which is allowed inside the class.
class MyClass: def _private_method(self): # Single underscore private method print("This is a private method") def public_method(self): self._private_method() obj = MyClass() obj.public_method() # obj._private_method()
Output
This is a private method
Double Underscore Private Method
We can use this double underscore private method __private_method to make an attribute or method less accessible from outside the class to avoid accidental modification.
Example
The Name mangling modifies the method's internal name to unique, like in the below example code _MyClass__private_method is used to access the private method.
class MyClass: def __private_method(self): print("This is a truly private method") def public_method(self): self.__private_method() obj = MyClass() obj.public_method() # Using name mangling to access the private method obj._MyClass__private_method()
Output
This is a truly private method This is a truly private method
Private v/s Public Methods
Some of the key differences between private and public methods are as follows.
Feature | Private Method | Public Method |
---|---|---|
Definition | Methods are accessible only within in class. | Methods are accessible from anywhere(within or outside a class). |
Name Convention | Single or double underscores (eg: def_method() or def__method()). | No leading underscores (eg: def method()). |
Access | Attributes/methods hidden outside the class. | Attributes/methods meant for open access. |
Purpose | Designed for internal use within the class to hide implementation details. | Designed for use by external code or subclasses. |