Page 82 -
P. 82
ready for pypi
Let’s test this. Staying at the IDLE shell, import your module, create the list, and then try to invoke the function
without a qualifying name. You’re expecting to see an error message:
>>> import nester
>>> cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman']
>>> print_lol(cast)
Traceback (most recent call last): As expected, your code has caused a
File "<pyshell#4>", line 1, in <module> NameError, because you didn’t qualify
print_lol(cast) the name.
NameError: name 'print_lol' is not defined
When you qualify the name of the function with the namespace, things improve dramatically:
>>> nester.print_lol(cast)
Palin
Cleese
Idle
This time, things work as expected…the data items in
Jones
the list are displayed on screen.
Gilliam
Chapman
Geek Bits
When you use a plain import statement, such as import nester,
the Python interpreter is instructed to allow you to access nester’s
functions using namespace qualification. However, it is possible to be
more specific. If you use from nester import print_lol, the
specified function (print_lol in this case) is added to the current
namespace, effectively removing the requirement for you to use
namespace qualification. But you need to be careful. If you already have
a function called print_lol defined in your current namespace, the
specific import statement overwrites your function with the imported
one, which might not be the behavior you want.
Your module is now ready for upload to PyPI.
46 Chapter 2