Friday, December 27, 2024
Google search engine
HomeLanguagesHow to create modules in Python 3 ?

How to create modules in Python 3 ?

Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using the pip installer, We can also create our own python module.

In this article, we will see how to create modules in python. 

Writing The Module

Any python code consisting of functions, classes, variables can be termed as a module. We will be writing a simple module that will be having a function, a class, and a variable.

Python




# defining a class Age
class Age:                    
    
    # defining the __init__ method
    def __init__(self, name, years):    
        self.years = years
        self.name = name
          
    # defining the getAge method
    def getAge(self):                    
        print("The age of " + self.name +" is "+self.years)
          
# defining the function hello       
def hello():                
  print('hello Lazyroar!')
    
# creating a string s 
s = 'I love python!'            


Here in the above module, we have defined a class ‘Age’ having a __init__() and getAge() method which will be used to initialize the class object and getting the age of the object that would be passed, We have also defined a function hello() which will print ‘hello Lazyroar!’ and also a string s which is ‘I love python!’.

We will name the above module made as mod.py and save it.

Importing The Module

Now we will discuss how to import a module and use its various functions, classes, variables from other python files. For this, we will make a file main.py in the same directory as mod.py. 

We will import the module with an import statement and use the functions, classes, variable as following:-

Using functions

For using the function from a module we will first import it and then will call the function with module_name.function_name(). For example, if we want to call the function hello() of mod.py in main.py we will first import mod.py and then call it with mod.hello().

Python




# importing the module mod.py
import mod         
  
# calling the function hello() from mod.py
mod.hello() 


Output:

hello Lazyroar!

Note: Please make sure that the main.py and mod.py are in the same directory.

We can also import only a separate function from a module to use it by writing: from module_name import function_name

And then call it directly. For example, if we want to call the function hello() of mod.py in main.py using the above method we will write the following code in main.py.

Python




# importing only the function hello from the module mod.py
from mod import hello         
  
# calling the function hello() 
hello()     


Output:

hello Lazyroar!

Using Classes

We can use the classes to make various objects by module_name.class_name(). For example, if we want to create an object in main.py by the name ‘jack’ of class Age present in mod.py. We will write the following code in main.py:

Python




# importing the module mod.py
import mod          
  
# creating a object jack with jack as name and 21 as age
jack = mod.Age('jack','21'
  
# calling the getAge() method for object 'jack'
jack.getAge()                


Output:

The age of jack is 21

The way we imported only a particular function in the same way we can import only a particular class. The code for this will be:

Python




# importing only the Age class from mod
from mod import Age          
  
# creating the object jack with jack as name and 21 as age
jack = Age('jack','21')    
  
# calling the getAge() method for the object jack
jack.getAge()                


Output:

The age of jack is 21

Using Variables

For getting any variable from a module we have to first import the module, and then we can simply get it by module_name.variable_name. For example, if we want to print the variable with name s present in our mod.py module we will write the following code:

Python




# importing the module mod.py
import mod          
  
# printing the variable s present in mod.py
print(mod.s)        


Output:

I love python!

As we did for functions and classes in the same way we can import a particular variable from a module. For printing variable ‘s’ by this method the code will be:

Python




# importing only the variable s from module mod.py
from mod import s         
  
# printing the variable s
print(s)    


Output:

I love python!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments