Hi. Welcome to another interesting session. If you are new please do checkout previous articles on python and other programming languages. And also subscribe to our YouTube channel for the video tutorials.
In this article you will learn:
- What is a module?
- How to create and import custom made modules in python
- Find the different ways to import and use custom built-in modules in Python.
Defining a module
Let’s start by defining modules in python
Modules contain statements and definitions in python that is, any file that contains proper Python code and has the .py extension can be called a Python module. They usually contain objects such as classes and functions.
Modules help break down large programs into manageable files. They also promote code reusability
Creating a module
Let us create our first module. Modules are generally known by their main filename, that is, without the .py extension. For example, let’s create a module and save it as multiplier.py.
def multiply (a, b):
product = a * b"""this programs multiply two numbersand returns the result"""return product
The module which we have just created will be known as a multiplier. The function above takes in two numbers and returns their product.
Importing a module
Importing a module allows us to access the objects, statements, and definitions it contains.
There are different ways to import a module.
Through a dot (.) operator and also by using by the import keyword
For example using the dot (.) operator, if you want to reuse the multiplier module, you can use the following statements:
>>>import multiplier
>>> multiplier.multiply (5, 3)15>>>
The answer which we will get above is 15. This is so because we have imported a multiplier which is a product rule, so the two numbers will be multiplied.
Note that, Python has different ways which we can use to Import modules in python. Check out the full list of python standard modules at the library directory inside the location where python is installed.
Renaming a module
We can import a module by renaming it. We can change the names of the already existing module. This actually saves us time and stress when the name of the already existing module is too long. Let’s take an example
>>> import mathematics as math
>>>print(math.sqrt(25))5.0>>>import multi as multiplier>>>print(“The product of 10 and 4 is “, multi.multiply(10, 4))The product of 10 and 4 is 40>>>
Universal Imports
Instead of importing specific object(s), it’s possible to import everything from a module by using an asterisk * in the import statement. This is called universal import.
>>> from math import *
>>> sin(4.01) + tan(cos(3.1)) + e>>> from math import *>>> sin(4.01) + tan(cos(3.1)) + e
Using the dir() built-in function
We can use the built-in function dir to list the names inside a module after importing it and supply the names as arguments.
For example, to view the contents of the user-defined multiplier module:
>>import multiplier
>>>dir (multiplier)[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’,‘__package__’, ‘__spec__’, ‘multiply’]>>>
Note that the dir() built-in functions can be used on all modules even the ones you have created
Importing several module at once
We can import several modules in one import statement by separating modules names with commas.
>>>import math, multiplier
>>>
We have come to the end of this session. Hope you were as excited as I am. Have a nice time coding.
Comments
Post a Comment
Please do not enter any spam link in the comment box.