Python - Modules

A module in Python is a file containing a set of functions, classes, and variables that can be imported and used in another program. The purpose of creating a module is to break a large program into smaller, more manageable pieces, which can be reused in other programs.

In Python, a module is loaded with the help of the import statement. Once a module is loaded, its functions, classes, and variables can be accessed using dot notation. For example, if we have a module named mymodule.py with a function named myfunc, we can load the module and call the function as follows:

import mymodule
mymodule.myfunc()

Python provides a number of built-in modules that can be used to perform various tasks, such as math, os, sys, random, and datetime, among others. Additionally, users can create their own modules and distribute them to others for use in their programs.

Here's an example of creating a simple module named mymath with a function that adds two numbers:

# mymath.py
def add_numbers(a, b):
    return a + b

We can then import this module in another program and use the add_numbers function:

import mymath
result = mymath.add_numbers(2, 3)
print(result)  # Output: 5

Modules are an essential part of Python programming and allow developers to create complex programs by breaking them down into smaller, more manageable parts.

The from...import statement in Python allows importing specific attributes or functions from a module, instead of importing the entire module. This statement is used when we only require a specific part of a module and don't want to use the entire module.

Here's the syntax for the from...import statement:

from module_name import name(s)

where module_name is the name of the module and name(s) is the name of the specific attribute or function we want to import from the module.

For example, let's say we have a module named math_operations that contains several mathematical operations. If we only want to import the addition function from this module, we can use the from...import statement as follows:

from math_operations import addition
result = addition(3, 5)
print(result)

In this example, we only import the addition function from the math_operations module. We then call the addition function with two arguments and store the result in the result variable. Finally, we print the result to the console.

Note that we don't need to use the module name before the function name when using the from...import statement, as we have already imported the specific function from the module.

In Python, a namespace is a collection of identifiers (names) and their corresponding objects. It provides a way to avoid naming conflicts and provides a clear and organized hierarchy for naming.

In Python, namespaces can be classified into two types: global and local. Global namespaces are those that are available throughout the program, while local namespaces are those that are limited to a particular block of code, such as a function.

Scoping is the set of rules that governs how names are looked up in Python. It specifies which variables are accessible in which parts of the code. Python uses the concept of lexical scoping, which means that the scope of a name is determined by its position in the code.

In Python, a variable declared inside a function is local to that function and cannot be accessed outside it. Similarly, a variable declared outside a function is global and can be accessed throughout the program.

Here is an example that demonstrates namespaces and scoping in Python:

x = 10  # global variable
def foo():
    x = 5  # local variable
    print("Local variable x =", x)
foo()
print("Global variable x =", x)
#Output:
#Local variable x = 5
#Global variable x = 10

In the above example, x is a global variable that is accessible throughout the program. Inside the foo() function, a local variable x is declared, which is different from the global variable x. When the foo() function is called, it prints the value of the local variable x. After calling the function, the global variable x is printed, which has not changed its value.