nuwc_nps_banner.png

Lesson 1.6: Namespace, Importing

Namespaces

Namespaces are dictionaries that map names (variable names, function names, etc.) to values/functions.

Python maintains multiple namespaces (variable/function dictionaries) defined/accessed within the following scopes. Each of these namespaces is created at different moments and has different lifetimes.

Namespaces are completely isolated (from each other).

You can have the SAME NAME (variable name, function name, etc.) stored SIMULTANEOUSLY in multiple namespaces *WITH DIFFERENT VALUES/FUNCTIONS!!!

Python will never be confused about which variable name it should use because...

Python Scope & Namespace Lookup Hierarchy

A scope is a textual region of a Python program where a namespace is directly accessible.

At any time during execution, there are several nested scopes whose namespaces are directly accessible:

When given a name, the Python interpreter searches from the "bottom" of the scoping hierarchy (i.e., the current location) to the "top".

Image Source: http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html

Some Important Concepts in Namespace

In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap. Once an object’s reference count drops to zero and it is garbage collected, then its identifying number becomes available and may be used again.

x = dict()
y = x

The normal way of business: variables in a function are local (exist only in the function)

The typical way to get around this is to pass in a reference to the object as a function argument

Interactions Between Scopes (Local, Global, Built-In)

It is possible to create/update a variable in the global scope (_main_ namespace) from within a function (although this can be dangerous to do).

It is possible to use a variable defined at a higher level in the lookup hierarchy (without passing it as an explicit parameter to a function).

It is possible to "override" the built-in namespace (this is very bad!).

You can always find out what variables are in the global scope in interactive mode.

removing items from the namespace with del

Importing modules, functions (into the current namespace)

Different Ways to Import Modules (and their effects)

Let's take a look at each of these one at a time.

Import random and call the randint(1,6) function inside the module

Import the glob module and call the glob("*") function within the glob module

Module names can be abbreviated like this:

Import pylab as plt and evaluate the following code (write it out, don't copy):

x = plt.arange(0,10,.1)
y = plt.sin(x)
plt.plot(x,y)
plt.show()

We can also import specific functions within a module

Now we can do some fun things...

And now the bad example of what you shouldn't do...

Only use this when you are writing temporary quick and dirty code and even then, use sparingly.

Activity - Write a function for a line array and import the numpy package:

$B_u(u)=\frac{1}{N}\frac{sin(\pi N u d/\lambda)}{sin(\pi u d/\lambda)}$

$N$: number of sensors
$\lambda$: Wavelength
$d$: distance between sensors
$u$: Normalized bearing between -1,1 ($u=cos \theta$)

Import numpy to use sin and $\pi$

Activity