What is __init__ in python and what does it mean?

In this post we’ll be learning about What is __init__ in python and what does it mean. In Python file names are quite different from other programming languages, often confuse developers and hard to understand.
Don’t worry we have all the answere for your question regarding python’s __init__.py file. So without wasting less time lets get started.

Table of Contents

Getting to know __init__.py file

The directory in which __init__.py file is located tells Python to marked that directory as Package. It may be empty or may contain some python code that could be used by package.

A Package is a collection of modules under a single directory. Each module may contain variables, functions and classes. A Package can also have a Sub-Package. We can create Sub-Package simply by creating __init__.py file inside package directory.

Every time the module inside package is called __init__.py file is excuted internally.
Below is a example of directory structure.

project
    |
    |
    - package_a # This is a main package
    |    |
    |    |
    |    - __init__.py
    |    - modules.py
    |    |
    |    - sub_package_1 # This is a sub package
    |    |    |
    |    |    |
    |    |    - __init__.py
    |    |    - sub_demo1.py
    |
    |
    - student.py # this file is outside package

Some Real-Life examples

Till now __init__.py file is left blank. We can also add some code to __init__.py file which will trigger each time when any thing is imported from package_a.
Till here we have created a directory structure now its time to write some code.
In package/modules.py file will write a class Person.

class Person:
    
    def __str__(self):
        return "{} | {}".format(self.name,self.age)

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def show(self):
        return "Name : {} \nAge : {}\n".format(self.name, self.age)

In student.py is a project file which is located outside package_a will import modules.py from package_a.
There are many ways to import package code given below.

import package_a.modules
p1 = package_a.modules.Person("Ravi", 21)
print(p1.show())

#PYTHON OUTPUT
Name : Ravi 
Age : 21

#-------------------------------------

from package_a.modules import Person
p1 = Person("Suresh", 23)
print(p1.show())

#PYTHON OUTPUT
Name : Suresh 
Age : 23

You directly cannot import functions or classes from package modules. To do that you must first call package.module and than import class or function from that module.

Sub-Packages

A Packages may also contain sub-packages and sub_package_1 inside package_a is a sub package.sub packages also need to have __init__.py file.
The sub_demo1.py file is a module inside sub-package which has function demo1.
In package_a/sub_package_1/sub_demo1.py file.

def demo1():
    print("printed from sub_package_1.demo1.py file")   

Importing sub-package in student.py file.

import package_a.sub_package_1.sub_demo1 
from package_a.sub_package_1 import sub_demo1
from package_a.sub_package_1.sub_demo1 import demo1 

package_a.sub_package_1.sub_demo1()
print('--------------------------\n')
sub_demo1.demo1()
print('--------------------------\n')
demo1()

#PYTHON OUTPUT
printed from sub_package_1.demo1.py file
--------------------------

printed from sub_package_1.demo1.py file
--------------------------

printed from sub_package_1.demo1.py file

Conclusion

We can reached at the end of our post on What is __init__ in python and what does it mean?. If you find anything difficult with our way of explanation please leave us a comment.

Related Posts

Summary
Review Date
Reviewed Item
What is __init__ in python and what does it mean?
Author Rating
51star
1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Langauage