Exploring the IronPython Modules
❘
37
An external module is one that you must load separately in order to use. When you create a file of
functions you want to use within your application, you must load that module and then provide
access to it from within your application. The following sections discuss both built-in and external
modules. However, you should consider these sections as an overview because you’ll use modules
throughout the book.
Working with the built-in Modules
As previously mentioned, built-in modules are those that come with IronPython. There are actually two
levels of built-in modules: those that are available immediately and those that you have to import first.
This second group is part of IronPython, but you don’t always need them, so the interpreter asks that
you import them before using them. The following sections discuss both levels of built-in modules.
Considering the Immediately Available Modules
When you start the IronPython interpreter, you get a few functions immediately. These functions
are internal to the interpreter itself. To see these functions, you use the
dir()
function. If you type
dir()
by itself, you see the top-level modules shown in Figure 2-18.
FIgURE 2-18:
Use dir() to obtain a list of modules.
The three names,
__builtins__
,
__doc__
, and
__name__
, probably don’t tell you very much.
(Yes, those are double underscores before and after each module name.) However, all the functions
you’ve used so far in the book appear in these three modules. Type
dir(__builtins__)
and press
Enter. Suddenly, you begin seeing function names that you’ve used before, as shown in Figure 2-19.
For example, you’ll find the
dir()
function in the list, as well as
raw_input()
. These functions
help you create basic applications without importing anything else.
It turns out that
dir()
is an exceptionally helpful function. For example, if you want to discover
the methods and attributes that the
raw_input
object supports, then you type
dir(raw_input)
. Go
ahead; give it a try right now. You’ll find out that
raw_input()
supports additional features such as
__format__
that we’ll use later in the book.
You may have noticed that the
__doc__
attribute keeps popping up in the lists that you display. The
__
doc__
attribute is also exceptionally important because it provides usage information about the object
or function in question. For example, you might want to find out more about
raw_input.__format__
()
, so you’d type
print raw_input.__format__.__doc__
and press Enter — the interpreter would
display the help information shown in Figure 2-20. In this case, you see that
__format__()
requires
an object that requires formatting and a string that tells how to format it.
548592c02.indd 37
2/24/10 12:47:22 PM
www.finebook.ir
Do'stlaringiz bilan baham: |