Stringing Statements Together


A basic IronPython application begins as a series of statements, as a procedure. These statements perform some task at the command line or within a window, depending on which IronPython interpreter you use. This initial execution path is called the main function, even though there really isn’t a function — simply a set of statements.

A lot of people feel uncomfortable with the open feel of IronPython’s main function so they create something that looks a bit more like what they’re used to using, as shown in Listing 3-1.

Listin g 3-1: Creating an actual main() function

import sys
# Create a main() function that everyone can recognize.
def main(argv = None):
# Obtain the command line arguments.
if argv is None:
argv = sys.argv
# Display the command line arguments.
for ThisArg in argv:
print ThisArg
# Pause after the debug session.
raw_input(‘Press any key to continue…’)
# Call the main() function.
if __name__ == “__main__“:

This code shows a few interesting structural features of IronPython, which is why it’s such a good example to start with. The code actually begins with a comparison of __name__ to “__main__“. It turns out that __name__ always contains the name of the current function. Using __name__ makes it possible for your application to detect its position in the execution loop.

Most programming languages provide an equivalent of the main() function described in Listing 3-1. Even C# and Visual Basic have a main()-type function that creates the initial form. Whether you actually use a main() function in your applications depends upon how you expect other developers to interact with your code. The examples in this book don’t rely on a main function for the sake of clarity, but you might want to consider including one when developers are used to working with main() as part of their coding experience. Of course, you can call main() anything you like to match the programming language that your developers normally use.

In this case, the application calls main(), the new main function. If you’ve spent any time writing C or C++ code, this version of main() will look a little familiar. While it lacks argc (the argument count), this main() does have an argv. However, the arguments can change between the time the code starts the application and begins processing main(), so you want to set argv to None (essentially, nothing at all in IronPython), and obtain the command line arguments from sys.argv.

At this point, the code uses a for loop to parse argv and displays its arguments on screen. At a minimum, you get the name of the application. The arguments you see depend on their order on the command line (or in Visual Studio). For example, if you specify -D after the filename, then you see it as part of the arguments. Figure 3-1 shows an example of what a series of arguments might look like when using a command line of -D -c These are arguments. The code ends by pausing so you can see the output when using the Visual Studio debugger.

Create a main() function and then process the command line arguments in it.Notice the levels of indentation in this example. The levels of indentation show the structure of the application. Unlike many application development languages, IronPython enforces indentation, with the result that you can see the application structure quite easily.

In IronPython, structure comes in a number of forms, all of which are accessible to you as the developer. Using structure properly makes your applications easier to understand. From the main (and any other) function, you can use the following structural elements:

  • Import external files
  • Call other functions
  • Use decision-making or loop structures
  • Interact with objects