Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Scientific Computing with Python - Second Edition

You're reading from  Scientific Computing with Python - Second Edition

Product type Book
Published in Jul 2021
Publisher Packt
ISBN-13 9781838822323
Pages 392 pages
Edition 2nd Edition
Languages
Authors (3):
Claus Führer Claus Führer
Profile icon Claus Führer
Jan Erik Solem Jan Erik Solem
Olivier Verdier Olivier Verdier
View More author details

Table of Contents (23) Chapters

Preface Getting Started Variables and Basic Types Container Types Linear Algebra - Arrays Advanced Array Concepts Plotting Functions Classes Iterating Series and Dataframes - Working with Pandas Communication by a Graphical User Interface Error and Exception Handling Namespaces, Scopes, and Modules Input and Output Testing Symbolic Computations - SymPy Interacting with the Operating System Python for Parallel Computing Comprehensive Examples About Packt Other Books You May Enjoy References
Namespaces, Scopes, and Modules

In this chapter, we'll cover Python modules. Modules are files containing functions and class definitions. The concept of a namespace and the scope of variables across functions and modules are also explained in this chapter.

The following topics will be covered in this chapter:

  • Namespaces
  • The scope of a variable
  • Modules

13.1 Namespaces

Names of Python objects, such as the names of variables, classes, functions, and modules, are collected in namespaces. Modules and classes have their own named namespaces with the same name as these objects. These namespaces are created when a module is imported or a class is instantiated. The lifetime of a namespace of a module is as long as the current Python session. The lifetime of a namespace of a class instance is until the instance is deleted.

Functions create a local namespace when they are executed (invoked). It is deleted when the function stops the execution with a regular return or an exception. Local namespaces are unnamed.

The concept of namespaces puts a variable name in its context. For example, there are several functions with the name sin and they are distinguished by the namespace they belong to, as shown in the following code:

import math
import numpy
math.sin
numpy.sin

They are indeed different, as numpy.sin is a universal...

13.2 The scope of a variable

A variable defined in one part of a program does not need to be known in other parts. All program units to which a certain variable is known are called the scope of that variable. We'll first give an example. Let's consider the two nested functions:

e = 3
def my_function(in1):
    a = 2 * e
    b = 3
    in1 = 5
    def other_function():
       c = a
       d = e
       return dir()
    print(f"""
          my_function's namespace: {dir()} 
          other_function's namespace: {other_function()}
          """)
    return a

The execution of my_function(3) results in:

my_function's namespace: ['a', 'b', 'in1', 'other_function'] 
other_function's namespace: ['a', 'c', 'd']

The variable e is in the namespace of the program unit that encloses the function my_function. The variable...

13.3 Modules

In Python, a module is simply a file containing classes and functions. By importing the file in your session or script, the functions and classes become usable.

13.3.1 Introduction

Python comes with many different libraries by default. You may also want to install more of those for specific purposes, such as optimization, plotting, reading/writing file formats, image handling, and so on. NumPy and SciPy are two important examples of such libraries, Matplotlib for plotting is another one. At the end of this chapter, we will list some useful libraries.

To use a library, you may either

  • load only certain objects from a library, for example, from NumPy:
from numpy import array, vander
  • load the entire library:
from numpy import *
  • or give access to an entire library by creating a namespace with the library name:
import numpy
...
numpy.array(...)

Prefixing a function from the library with the namespace gives access to this function and distinguishes this function from other objects with the same name.

Furthermore, the name of a namespace can be specified together with the import command:

import numpy as np
...
np.array(...)

Which...

13.3.2 Modules in IPython

IPython is used in code development. A typical scenario is that you work on a file with some function or class definitions that you change within a development cycle. To load the contents of such a file into the shell, you may use import but the file is loaded only once. Changing the file has no effect on later imports. That is where IPython's magic command run enters the stage.

The IPython magic command – run

IPython has a special magic command named run that executes a file as if you were running it directly in Python. This means that the file is executed independently of what is already defined in IPython. This is the recommended method to execute files from within IPython when you want to test a script intended as a standalone program. You must import all you need in the executed file in the same way as if you were executing it from the command line. A typical example of running code in myfile.py is:

from numpy import array
...
a = array(...)

This script file is executed in Python by exec(open('myfile.py').read()). Alternatively, in IPython the magic command run myfile can be used if you want to make sure that the script runs independently of the previous imports. Everything that is defined in the file is imported into the IPython workspace.

13.3.3 The variable __name__

In any module, the special variable __name__ is defined as the name of the current module. In the command line (in IPython), this variable is set to __main__. This fact allows the following trick:

# module
import ...

class ...

if __name__ == "__main__":
   # perform some tests here

The tests will be run only when the file is directly run, not when it is imported as, when imported, the variable __name__ takes the name of the module instead of __main__.

13.3.4 Some useful modules

 

 

The list of useful Python modules is vast. In the following table, we have given a very short segment of such a list, focused on modules related to mathematical and engineering applications (Table 13.2):

Module

Description

scipy

Functions used in scientific computing

numpy

Support arrays and related methods

matplotlib

Plotting and visualization 

functools

Partial application of functions

itertools

Iterator tools to provide special capabilities, such as slicing to generators

re

Regular expressions for advanced string handling

sys

System-specific functions

os

Operating system interfaces such as directory listing and file handling

datetime

Representing dates and date increments

time

Returning wall clock time

timeit

Measuring execution time

sympy

Computer arithmetic package (symbolic computations...

13.4 Summary

We started the book by telling you that you had to import SciPy and other useful modules. Now you fully understand what importing means. We introduced namespaces and discussed the difference between import and from ... import *. The scope of a variable was already introduced in Section 7.2.3: Access to variables defined outside the local
namespace
, but now you have a more complete picture of the importance of that concept.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Scientific Computing with Python - Second Edition
Published in: Jul 2021 Publisher: Packt ISBN-13: 9781838822323
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}