Private Methods in Python
Last Updated :
07 Sep, 2022
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP) in Python. 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.
Private functions in Python
Consider a real-life example, a car engine, which is made up of many parts like spark plugs, valves, pistons, etc. No user uses these parts directly, rather they just know how to use the parts which use 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 the double underscore “__”. Note: The __init__ method is a constructor and runs as soon as an object of a class is instantiated.
Python3
class Base:
def fun( self ):
print ( "Public method" )
def __fun( self ):
print ( "Private method" )
class Derived(Base):
def __init__( self ):
Base.__init__( self )
def call_public( self ):
print ( "\nInside derived class" )
self .fun()
def call_private( self ):
self .__fun()
obj1 = Base()
obj1.fun()
obj2 = Derived()
obj2.call_public()
|
Output:
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.
Example:
Python3
class A:
def fun( self ):
print ( "Public method" )
def __fun( self ):
print ( "Private method" )
def Help ( self ):
self .fun()
self .__fun()
obj = A()
obj. Help ()
|
Output:
Public method
Private method
Name mangling
Python provides a magic wand that 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 the class name is the current class name with a leading underscore(s) stripped.
Example:
Python3
class A:
def fun( self ):
print ( "Public method" )
def __fun( self ):
print ( "Private method" )
obj = A()
obj._A__fun()
|
Output:
Private method
Share your thoughts in the comments
Please Login to comment...