Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.
Now, there can be some scenarios in which we need to put restrictions on some methods of the class, so that they can neither be accessed outside the class nor by any subclasses. To implement this private methods come into play.
Consider a real life example, a car engine, which is made up of many parts like spark plug, valves, piston, etc. No user use these parts directly, rather they just know how to use the parts which uses them. This is what private methods are used for. It is used to hide the inner functionality of any class from the outside world.
Private methods are those methods that should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private methods that cannot be accessed except inside a class. However, to define a private method prefix the member name with double underscore “__”.
__init__ method is a constructor and runs as soon as an object of a class is instantiated.
Public method Inside derived class Public method
Traceback (most recent call last): File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in
obj1.__fun() AttributeError: 'Base' object has no attribute '__fun' Traceback (most recent call last): File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private() File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun() AttributeError: 'Derived' object has no attribute '_Derived__fun'
The above example shows that private methods of the class can neither be accessed outside the class nor by any base class. However, private methods can be accessed by calling the private methods via public methods.
Public method Private method
Python provides a magic wand which can be used to call private methods outside the class also, it is known as name mangling. It means that any identifier of the form __geek (at least two leading underscores or at most one trailing underscore) is replaced with _classname__geek, where classname is the current class name with leading underscore(s) stripped.
- Private Variables in Python
- Access Modifiers in Python : Public, Private and Protected
- Python program to determine if the given IP Address is Public or Private using ipaddress module
- List methods in Python
- Bound methods python
- List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
- Web Driver Methods in Selenium Python
- Dunder or magic methods in Python
- Accessing Attributes and Methods in Python
- Methods of Ordered Dictionary in Python
- Python | Float type and its methods
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Accessor and Mutator methods in Python
- Response Methods - Python requests
- Element methods in Selenium Python
- Bound, unbound, and static methods in Python
- Http Request methods - Python requests
- Python | Implementing 3D Vectors using dunder methods
- Python Input Methods for Competitive Programming
- Dictionary Methods in Python | Set 2 (update(), has_key(), fromkeys()...)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.