Whether you are an expert or a beginner in Python programming, you must be familiar with import module commands. However, as a newcomer you may have no idea where the mod modules came from.
Let’s take a closer look at the origins of all those modules, including how to create and import them for reuse.
What is code reusability and why should you do it?
One of the best practices when working with most server-side languages is to modulate your code so that it is reusable. Python is object-based, one of the languages that makes code reusable.
Code modularization includes written codes that follow different instructions in the block. It is used to separate the work from each other.
When you modulate a code, you give it a unique name. This name is its identity and it means next time you need to use that block of code then you just have to call its name, not the whole code.
This practice makes your work much easier and faster during a real life project. Code modulation also improves the speed of implementation and makes testing easier. In short, it makes your code more efficient and readable.
Although our example here is not based on Object-Oriented Programming (OOP), we need to briefly discuss it before proceeding to create a module.
The code you sometimes want to reuse may stand alone as a separate function. However, it can also be in the form of a method in any class. That’s when the idea of OOP came into play.
Object-oriented programming in Python
The presentation of the code as an independent object in the form of a class is OOP. Then each object has its properties and methods.
These properties are class properties, while each method determines the behavior of the properties.
Instead of making long code less effective — a convention of systematic programming — your code becomes more efficient and specific to OOP. The reason is that in systemic programming the function and data are stored in separate classes in OOP as opposed to the isolation of both.
Then each class you create takes on an identity. So when you need a class named module you call it with class reference.
For a better understanding, see our beginner’s guide to understanding OOP.
What is Object Oriented Programming? The key points are explained in Lehman’s terms
Most modern programming languages support the “Object-Oriented Programming” (OOP) instance. But what exactly is OOP and why is it so effective?
Performing reusable tasks: A practical example
Let’s take a look at how we can reuse the function for word counters in another Python file. This guide will only focus on creating reusable functions that are not contained within an object.
First, open a command prompt anywhere on your computer to start a new project. In this case, we will use a project name of word_count. To do this, type mkdir word_count.
Next, use the virtual environment tool of your choice to create a new virtual environment. Only activate if you have already created one. Make sure you are still in your project directory.
As a good practice, to create word counter words, we first try to figure out any raw formula to calculate it. Typically, you can find a word count by adding one to a blank number in a sentence. Keep in mind that you may not need to count the words, the concept is related to the reusability of the code which is important for this interpretation.
Next, open a text editor at your project location and create a new Python file. In this case, the file is named wordcounter.py; Make sure you use the correct .py extension.
Here’s how to view the WordCounter file:
# Create a word called a variable that contains some strings
Word = [‘How to counter word in Python]]
# Next, loop through the variable to count the words in the created variable
Number Offwords = [1 for each wordcont (”)) word for each word]
Now that the raw code is working, we then modulate it by creating a function that makes the code reusable:
Def count (words):
If the word is not:
Number ofwards = [codeout for each word for each word (”) + 1]
MyWords = [‘Changing a word to a python’]
Print (countwords (mywords))
That’s it; We have created a word counter module. Let’s see how to reuse it.
The created module is being imported
Note that you have previously created a file called WordCounter.p. This file contains a function called Countwords. If you need that function in a new file and don’t want to overwrite the whole code or function, all you have to do is import that function as a module in your new file.