Page 107 -
P. 107
sharing your code
Your Python Toolbox
You’ve got Chapter 2 under your
belt and you’ve added some key
Python goodies to your toolbox. A module is a text file that contains Python
code. CHAPTER 2
The distribution utilities let you turn your
Python Lingo module into a shareable package.
The setup.py program provides
• Use a “triple-quoted string” to include metadata about your module and is used
a multiple-line comment in your code. distribution.
to build, install, and upload your packaged
• “PyPI” is the Python Package Index and Import your module into other programs
is well worth a visit. using the import statement.
• A “namespace” is a place in Python’s Each module in Python provides its own
memory where names exist. namespace, and the namespace name
is used to qualify the module’s functions
• Python’s main namespace is known as when invoking them using the module.
__main__. function() form.
Specifically import a function from a module
into the current namespace using the from
IDLE Notes module import function form of
the import statement.
• Press F5 to “run” the code in the IDLE Use # to comment-out a line of code or
edit window. to add a short, one-line comment to your
program.
• When you press F5 to “load” a module’s
code into the IDLE shell, the module’s The built-in functions (BIFs) have their own
namespace called __builtins__,
names are specifically imported into
which is automatically included in every
IDLE’s namespace. This is a convenience
Python program.
when using IDLE. Within your code, you
The range() BIF can be used with for to
need to use the import statement
explicitly. iterate a fixed number of times.
Including end=’’ as a argument to the
print() BIF switches off its automatic
inclusion of a new-line on output.
Arguments to your functions are optional if
you provide them with a default value.
you are here 4 71