Skip to main content

Python Custom Modules


In this tutorial you will learn:

  • Modules
  • Modules in Python
  • Creating your own Modules
  • Using your own Modules
  • Finding contents of a Module

Modules

We have been using Modules for quite a while now. We noticed that after importing the modules we had access to more functions in our code. So a Module is a package that contains different sets of functions, classes and variables. Whenever be bring the module in our code we get those extra functions by just writing a single line of code at the top.

Modules in Python

Modules in Python also contain functions and statements. We use modules to split the code into separate files which could then be imported to another file with just one line import statement. Any Python file can serve as a Module. It gives us a lot of flexibility and allow code reusability.

Creating your own Modules

Creating a Module in Python is very simple we can simply create a file with .py extension and then import the file into another file. Let’s create a file called “calculator.py”

Example:

Let's create a simple printing modules that prints items of a listing using for loop. Save this in a file named "printing.py"

  1. def printElementsOf(list):
  2. for element in list:
  3. print("Element is ", element)
  4.  
  5. def printGreetings():
  6. print("Hello user welcome to printing module")

Now let's make a simple calculator module and name it "calculator.py"

  1. def multiply(x, y):
  2. return x * y
  3. def divide(x, y):
  4. return x / y
  5. def subtract(x, y):
  6. return x - y
  7. def add(x, y):
  8. return x + y

Let's create a third module and name it "fib_fact.py". It will print fibonacci and factorial of a given number.

  1. def factorial(num):
  2. if num == 1:
  3. return 1
  4. else:
  5. return num * factorial(num-1)
  6.  
  7. def fibonacci(n):
  8. if n <= 1:
  9. return 1
  10. else:
  11. return(fibonacci(n-1) + fibonacci(n-2))

Using your own Modules

Now let’s use our newly created modules in our new created Python file. We know that we first need to import the Module and then we will have access to all the methods and functions of that module.

Example:

There are different ways to import a module so we will use different ways for every module we created. Using the printing module we simply use the import statement.

  1. import printing
  2. print("\n\n** Printing Module ** ")
  3. printing.printGreetings()
  4. myList = ["Test", "Python", "Programming"]
  5. printing.printElementsOf(myList)

For calculator module we will using the from statement. It allows us to import only specific functions from the module.

  1. from calculator import multiply, divide, add, subtract
  2. print("\n\n ** Calculator import using from **")
  3. print("Sum of 5 and 6", add(5,6))
  4. print("Subtraction of of 20 and 5", subtract(20,5))
  5. print("Multiplication of 10 and 4", multiply(10,4))

We can also import the module and then rename it. Let's use this for importing our third module.

  1. import fib_fact as ff
  2. print("\n\n ** Fib Fact renamed import **")
  3. print("Factorial of 4", ff.factorial(4))
  4. print("Fibonacci of 10", ff.fibonacci(10))

Finding contents of a Module

To find the contents of a Module is very simple we simply need to call the dir function by passing in the module name.

Example:

  1. import calculator<br />
  2. print(dir(calculator))

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.