Share via


Learning IronPython #4: Using the Console, Calling the dir Function, Using the import Statement, and Exploring the sys Module

Now that you know how to start the console, let's start giving IronPython something to do.

At the console prompt (make sure you see >>> at the command prompt, which means IronPython is ready to accept your commands), type the following (except for the >>> part, of course) and be sure to press the Enter key after you finish typing:

>>> dir()

The console displays the following:

['__builtins__', '__dict__', '__doc__', '__name__', 'site']

IronPython's built-in dir function returns the list of names that a module defines. All built-in functions, exceptions, and so on exist in IronPython's built-in __builtins__ module. So, typing dir() is the same as typing __builtins__.dir().

Now type the following:

>>> dir(__builtins__)

The console now displays the following:

['ArithmeticError', ... (omitted for brevity) ... 'zip']

These are all of IronPython's built-in functions, exceptions, and so on.

Now type the following:

>>> import sys
>>> dir(sys)

The console displays the following:

['LoadAssemblyByName', ... (omitted for brevity) ... 'warnoptions']

If you tried to type dir(sys), you'd receive the following message:

Traceback (most recent call last):
File , line 0, in <stdin>##0
NameError: name 'sys' not defined

This is because unlike the __builtins__ module, although the sys module is always available, it must be imported first (similar to a #include statement in C++ or an imports statement in C#). To get the list of all built-in modules that are available with this version of IronPython, type the following:

>>> sys.builtin_module_names

The console displays the following:

('binascii', ... (omitted for brevity) ... 'cStringIO')

I found this documentation for common built-in Python modules and their corresponding functions. There are many more Python modules listed there than are available with this version of IronPython. I'm guessing that over time there will be more parity between IronPython and Python. I'm sure it's simply a matter of time and resources.

-- Paul

------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.