Here we can see that our file is blue, and the modules named math, random and time are all represented in different colors.
When we import functions from these modules, although they "live" in their own boxes, we make some room in our "home" for them.
Using the statements as shown here, if we want to call the sleep() function, we just use:
sleep(1)
to 'sleep' our program, or make it wait for one second.
Another way to do it is this:
import time
import random
import math
To visualise what is happening, it is as shown in the diagram on the right. We've made "space" in our file for these three modules (files).
Now, the functions we might want to use 'live' or exist inside these boxes... so now if we want our program to pause or wait or sleep for one second, we say :
time.sleep(1)
Because now the function is NOT just sitting in our program, but sitting in a box in our program, and we address this by saying it is in the time box and it is called sleep(). The 1 is passed as an arg to the sleep function, and represents one second.
So... when we write functions as shown here, we can just call them in our program
dbl = my_func(7)
answer = another(False)
But to access functions in the imported modules we still have to use the address of where the function lives:
lotto_ball = random.randrange(1, 44)
Note on local re-naming (nickname)
Another way to import a function from a module is to give it a local name. In the example shown, sleep is imported as slp. This means that the sleep function from the time module is known locally as slp... and the function known formerly as sleep is unknown. It's like inviting cousin Ezeekiel to a party and introducing him to everyone as Zeek. No-one would know who Ezeekiel would be (and programs do not guess!)
>>> from time import sleep as slp
>>> slp(1)
>>> sleep(1)
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
NameError: name 'sleep' is not defined
We can also import an entire module with a nickname
>>> import longmodulename as lmm
Now to access function funky from the longmodulename, we use lmm.funky()
Namespaces are the constructs used for organising the names assigned to the objects in a python program. Let's build up to getting the gist what a namespace is.
A name or identifier is the name given to objects when we create objects in python. The name is simply the variable name that we use in our programs. In python, we can declare variable names and assign them to the objects as follows.
my_num = 11
my_str = "Python Rocks!"
Here, we have defined two variables having names my_num and my_str, which point to an integer and a string object respectively.
The video gives a nice analogy of namespace using people and houses. One way to think of a namespace is the location of objects- we could have an encrypt and a decrypt function in several files called (for example) morse, vigenere, caesar. Each function is different and it could be confusing if all three were "in the room" together. We can differentiate between them by referring to caesar.encrypt, morse.decrypt and so on. Namespaces let us compartmentalise "things".
This article goes into an in-depth discussion of namespaces.
Technically, a namespace is a collection of names and the details of the objects referenced by the names. We can consider a namespace as a python dictionary which maps object names to objects. The keys of the dictionary correspond to the names and the values correspond to the objects in python.
In python, there are four types of namespaces, namely built-in namespaces,global namespaces, local namespaces and enclosing namespaces.