In this tutorial, we will learn about the Module in Python. Its types, and how to use them with the help of examples

Module is the collection of function we define function within module because of we want to use the certain function in any python program simple by importing the module & use the related function.

Steps for Creating Module in Python

  • Create a new program File.
  • Define the function one by one in program file.
  • Save the file as – ModuleName.py

Example of Module in Python

# mymodule.py

def my_function():
    print("This is a function in mymodule")

class MyClass:
    def __init__(self):
        print("This is a class in mymodule")

my_variable = "This is a variable in mymodule"
Code

Let’s run the code

Example of Module in Python
Example of Module in Python
import mymodule

mymodule.my_function()
# Output: This is a function in mymodule

obj = mymodule.MyClass()
# Output: This is a class in mymodule

print(mymodule.my_variable)
# Output: This is a variable in mymodule
code

Let’s run the code

Example of Module in Python
Example of Module in Python

Output Value

Output Value

Constructor

Constructor is the special member function provided by OOPs concept. It is special because of invoke or call automatically when you crate an object of the class. Construction can be call only one time for each object of the class & hence. It can be use for variable initialization, resource opening like file Opening/ database opening, etc. The task of constructors is to initialize and assign value to the data members of the class, when an object of the class is created.

Syntax of Constructor in Python

class ClassName:
def __init__(self,variables...):
##body
Syntax of Constructor in Python

In Python, __init__() is the Constructor non-parameterized but cannot return value. It is always be public.

Example of Constructor in python

class Addition:
    def __init__(self, a, b): 
        self.key1 = a 
        self.key2 = b
   
    def calculate(self):
        print(self.key1 + self.key2)
obj = Addition(5632, 58745) 
    
obj.calculate() 
A Example of Constructor in python

Let’s run the code

Example of Constructor in python
Example of Constructor in python

Destructor In Python

Python Destructor is also a special method that gets executed automatically when an object exit from the scope. To destroy the object use del. Destructors are called when an object gets destroyed, it is opposite to constructors. When we destroy the object the del() function executed. When multiple reference variables are pointing to same object , if all reference variable count will become zero then only del() will be executed.

Syntax of Destructor In Python

class ClassName:
def __del__(self,):
##body
Syntax of Destructor In Python

Example

class MyClass:
    def __init__(self):
        print("Object created")

    def __del__(self):
        print("Object deleted")

obj = MyClass()
del obj
Example of Destructor In Python

Let’s run the code

Example of Destructor In Python
Example of Destructor In Python

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *