Skip to content
Related Articles
Open in App
Not now

Related Articles

Is __init__() a private method in Python?

Improve Article
Save Article
  • Last Updated : 21 Nov, 2022
Improve Article
Save Article

Here in this article we are going to find out whether __init__() in Python is actually private or not. So we might come across many questions like

  • What actually is __init__() method?
  • What actually are private methods?
  • And, if __init__() is private then how can we access it outside of a class?

We have already heard about the concept that private methods cannot be accessed outside the class they are declared in and also cannot be accessed from their base class but if this is true then how can we justify that we call the __init__() method every time we create the object of the class that too outside of the class and with the help of super keyword or using the class name we can also call the __init__() method of the parent class.

Accessing __init__() in Python

We need a constructor to initialize the data members of the class when a class is instantiated. Similar to methods, a constructor also contains the collection of statements that are executed as soon as the Object is created. The init is always called whenever we initiate a class.

Python3




# initialising class
class Demo:
   
    # defining a constructor
    def __init__(self, name):
 
        self.name = name
        print("Initialised value is", self.name)
 
 
# Driver code
obj1 = Demo("GFG")
obj2 = Demo("Geeks")
obj3 = Demo("GeeksForGeeks")


Output:

Initialised value is GFG
Initialised value is Geeks
Initialised value is GeeksForGeeks

So if __init__() is a private method then how can we access it outside of the class? So let’s check some more proofs for the same.

Accessing the __init__() for another class

Here we are trying to access one constructor from another constructor of another class.

Python3




# initialising class
class Demo:
 
    # defining a constructor
    def __init__(self):
        print("Init for base class")
 
 
class child(Demo):
 
    def __init__(self):
        Demo.__init__(self)
        print("Init for the child class")
 
 
# Driver code
obj1 = Demo()
obj2 = child()


Output:

Init for base class
Init for base class
Init for the child class

So here also we can see that we can access the constructor for base class from child class which violates the property of private methods.

Private methods in Python

Here we are trying to access a private method outside a class and we get an error for the same, but in Python, we can access a private method also using the concept of Name Mangling.

Python3




# Creating a class
class Demo:
 
    # Declaring public method
    def f(self):
        print("Public method")
 
    # Declaring private method
    def __f(self):
        print("Private method")
 
 
# Driver's code
obj = Demo()
obj.f()
 
print("Using the concept of name mangling")
obj._Demo__f()
 
print("Without using name mangling")
obj.__f()


Output:

Public method
Using the concept of name mangling
Private method
Without using name mangling

Traceback (most recent call last):
  File "main.py", line 20, in <module>
    obj.__f()
AttributeError: 'Demo' object has no attribute '__f'

Conclusion

So we can conclude that __init__ is not a private method, In Python, there’s nothing like private/protected by technique, it’s just a convention, and we can access private/protected methods. It’s more like a convention, rather than a technique. It is advised that we should follow this convention for better understanding.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!