Modules In Python

We will discuss Python modules through examples today so as to more effectively meet Python developer requirements.

Python Modules – What are they?

As we discuss Python modules, we refer to them as code libraries.

You can implement a set of functions in your application by making a file having them.



Implementing a Module

When it comes to Python modules, we can now utilise the module we just developed by using the import statement: a module

After importing the module mrxmodule, invoke the welcome function:

Example: 

import mrxmodulemrxmodule.welcome("Harry")

Reminder: It is possible to use the function of a module by using the following syntax: module_name.function_name.


Built-in Modules

Python comes with a number of built-in modules that you can import whenever you want.

Implement the platform module by importing it and utilizing it as follows:

Example: 

import platformmrx = platform.machine() print(mrx)

Creating Module

You can define a module by storing your code in a .py file.

Make a file titled mrxmodule.py and save the code there:

def welcome(name):
    print(f"Hey {name} Welcome to Python Module")

Import From Module

By utilizing the from keyword, you can import only parts of a module.

Save the below code in the file mrxmodule.py:

def welcome(name):
    print(f"Hey {name} Welcome to Python Module")

def player_bio(name, prof, age, country):
    bio = {
        "Player Name": name,
        "Profession": prof,
        "Age": age,
        "Country": country
    }
    return bio

Specifically, import the player bio function from the mrxmodule:

Example: 

from mrxmodule import player_bioprint(player_bio("Harry", 19, "Football", "United States of America"))

Reminder: Don’t refer to elements in a module by its name when importing using the from keyword.Example: player_bio(“Harry”, 19, “Football”, “United States of America”), instead of m̶r̶x̶m̶o̶d̶u̶l̶e̶.p̶l̶a̶y̶e̶r̶_̶b̶i̶o̶(̶”H̶a̶r̶r̶y̶”, 1̶9̶, “F̶o̶o̶t̶b̶a̶l̶l̶”, “U̶n̶i̶t̶e̶d̶ S̶t̶a̶t̶e̶s̶ o̶f̶ A̶m̶e̶r̶i̶c̶a̶”)̶]


Variables in Module

When we are talking about Python modules, they can include functions, as already explained, as well as variables of all types (arrays, dictionaries, objects, etc).

In the file mrxmodule.py, save the following code:

def player_bio(name, prof, age, country):
    bio = {
        "Player Name": name,
        "Profession": prof,
        "Age": age,
        "Country": country
    }
    return bio

The bio dictionary can be used by importing the module called mrxmodule:

Example: 

import mrxmodulemrx = mrxmodule.player_bio("Harry", 19, "Football", "United States of America") print(mrx)

Naming a Module

In Python modules, A file can have any name as per your desire, but it must have the .py file extension.

Re-naming a Module

The as keyword can be utilized to make an alias when importing a module:

Make an alias for mrxmodule named mrx:

Example: 

import mrxmodule as mrxample = mrx.player_bio("Harry", 19, "Football", "United States of America") print(ample)

Using dir() Function

It is possible to list all the function names (or variable names) in a module with a built-in function.

The following list contains all the names specified in the platform module:

Example: 

import platformmrx = dir(platform) print(mrx)

Reminder: All modules, including those you make yourself, can utilize the dir() function.


Python Module Importance

Modules are a fundamental concept in Python programming, offering numerous advantages and applications:

  1. Modules enable the organization of code into distinct files or units. By dividing code into modules, related functions, classes, and variables can be grouped together, simplifying codebase comprehension, navigation, and maintenance. Importing functionality from one module to another promotes code reuse.
  2. Modules establish separate namespaces to prevent naming conflicts. Each module possesses its own scope, preventing clashes between variables, functions, and classes that share the same name but exist in different modules. This enhances code modularity and minimizes the risk of unintended side effects.
  3. Modules permit the encapsulation of implementation details within a module while exposing only necessary interfaces to the outside world. Selectively importing specific functions or classes from a module conceals the underlying implementation, presenting a clean and simplified interface to other program components.
  4. Modules facilitate code sharing and collaboration among developers. Code can be packaged into modules for easy distribution and reuse across various projects. Additionally, Python’s extensive module ecosystem provides a wide array of pre-built modules and libraries that can be effortlessly integrated into custom projects.
  5. Modules contribute to performance optimization by allowing the import and usage of compiled code modules or modules written in other languages. Specialized modules like NumPy or Pandas, designed for numerical computations and data analysis, can be employed for computationally intensive tasks, resulting in faster execution times.
Subscribe to our Newsletter and stay up to date with the latest technical news and information.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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